Ir para conteúdo
Fórum Script Brasil
  • 0

Problema com ponteiros


RSsakatauskas

Pergunta

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++);

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

    if(*string == ch)return string;

    return NULL;

 }

Mas acaba aparecendo segmentation fault;

Alguém poderia me ajudar?

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 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
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   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
#endif

/* 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)
#endif

libc_hidden_builtin_def (strrchr)

 

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...