I am trying to initialize s1 with the value and later in squeeze() . I am changing s1. Pointer initialization throws a seg fault but array version works fine. Could someone tell me If i can do it with the pointer version and what is wrong with this ? may be because i have not used malloc ??
int main() {
char s1[] = "xyz abc zzz" ; // this works fine.
// char *s1 = "xyz abc zzz"; // if i initialize like this it throws a seg fault.
char *s2 = "abx xxx xxx" ;
squeeze(s1,s2) ;
puts (s1) ;
return 0 ;
}
void squeeze(char *s1, char *s2 ) {
int i , j, k = 0;
for (i=0 ; s1[i] != '\0' ; ++i) {
for (j=0; s2[j] != '\0'; j++) {
if (s2[j] == s1[i])
break;
}
if(s2[j] == '\0')
s1[k++] = s1[i] ;
}
s1[k++] = '\0' ;
}