1
I wrote this function to remove the characters nay repeated from any string in c:
char *repeticoes(char *s){
int i=0, j, cont=0;
while(s[i]!='\0'){
for(j=0;j<strlen(s);j++)
if(s[j]==s[i] & j!=i)
break;
if(j==strlen(s))
for(j=i;j<strlen(s);j++)
s[j]=s[j+1];
else
i++;
}
return s;
}
When I pass to it as a parameter an array s, s[20]="felicidade"
, I have the expected output that would be "eiidde"
, but when I pass as a parameter a vector *s="felicidade"
or simply "felicidade"
and have compiled, the program does not seem to respond.
Works normally:
int main(void){
char s[20]="felicidade";
printf("%s",repeticoes(s));
}
do not work:
int main(void){
char *s="felicidade";
printf("%s",repeticoes(s));
}
int main(void){
printf("%s",repeticoes("felicidade"));
}
it is because
*s
is a pointer on which you must "point" to a memory position, when you pass directly "happiness" this is not allocated in memory, ie do not have a position yet.– Guilherme Lautert