c - Calling a function in another function: segmentation fault(core dumped) -
i'm supposed function erases repeated characters in string. here's code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define dim 100 char* mystrcpy(char [], char[]); char* repeticoes(char []); int main() { char str1[dim]="john joanne"; printf("%s", repeticoes(str1)); return exit_success; } char* mystrcpy(char _dest[], char _orig[]) { int i=0; (i=0; _orig[i]!='\n'; i++) _dest[i]=_orig[i]; _dest[i]='\0'; return _dest; } char* repeticoes(char _s[]) { int i=0,j=0; char news[dim]={'\0'}; (i=0, j=0; _s[i]!='\0'; i++) { if(mystrcnt(_s, _s[i])==1) news[j++]=_s[i]; } news[j]='\0'; mystrcpy(_s, news); return _s; }
mystrcpy function created in previous exercise. problem i'm obtaining segmentation fault(core dumped). if try use string library function strcpy program runs ok. problem functions? not declaring/initializing/passing arguments strings correctly?
thanks!
edit: forgot indicate str1.
in function mystrcpy
loop -
for (i=0; _orig[i]!='\n'; i++) //can cause accessing index out of bounds
why check '\n'
, append '\0'
later ? once check _orig
contain '\n'
, understand.
you can run loop till '\0'
, copy.
Comments
Post a Comment