0
People don’t know where the bug is. Every time I compile it accuses error of Segmentation fault (core dumped), can anyone help me? The code is here:
#include <stdio.h>
int strlen(char *s);
char *strrev(char *s);
int main() {
char frase1[30+1];
char frase2[30+1];
printf("Digite uma frase: ");
gets(frase1);
printf("Digite outra frase: ");
gets(frase2);
printf("Frase 1 = %s\n", strrev(frase1));
printf("Frase 2 = %s\n", strrev(frase2));
return 0;
}
int strlen(char *s) {
int i;
while(s[i]) {
i++;
}
return i;
}
char *strrev(char *s) {
int i, len;
char aux;
for(i=0,len=strlen(s)-1; i<len; i++, len--) {
aux = s[i];
s[i] = s[len];
s[len] = aux;
}
return s;
}
Possible duplicate of Simple Teaching of Pointers
– Guilherme Nascimento
In
strlen
, you did not define the initial value ofi
, so he’s memory junk– Jefferson Quesado
Another alternative would be that the sentence size is too large
– Jefferson Quesado
Caraca hadn’t even noticed , @Jeffersonquesado pela ajuda.
– Patrick Cardoso
@Patrickcardoso I transformed in response to stay stored the correct history
– Jefferson Quesado
@Patrickcardoso, if the answer (born of the comment) solves your problem in the correct way, please mark as the answer accepted.
– Jefferson Quesado