Jump to content
Fórum Script Brasil
  • 0

Problema com ponteiros



Estou fazendo um exercício de implementar a função strrchr usando ponteiros e aparentemente parece que ia funcionar:


char *strrch(char *string,char ch)


    char *aux = string;


    while(*string-- !=  ch) || string != aux);

    if(*string == ch)return string;

    return NULL;


Mas acaba aparecendo segmentation fault;

Alguém poderia me ajudar?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Esse seria o código completo da função strrchr:

/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <string.h>

#undef strrchr
#ifndef STRRCHR
# define STRRCHR strrchr

/* Find the last occurrence of C in S.  */

char *
STRRCHR (const char *s, int c)
    const char *found, *p;
    c = (unsigned char) c;
    /* Since strchr is fast, we use it rather than the obvious loop.  */
    if (c == '\0')
        return strchr (s, '\0');
    found = NULL;
    while ((p = strchr (s, c)) != NULL)
        found = p;
        s = p + 1;
    return (char *) found;

#ifdef weak_alias
#undef rindex
weak_alias (strrchr, rindex)

libc_hidden_builtin_def (strrchr)


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Forum Statistics

    • Total Topics
    • Total Posts
  • Create New...