-
April 27th, 2013, 05:10 PM
#1
initialization of a pointer
I have to write a function,which gets a string,deletes its spaces and returns the result..That's what I did:
char *removespaces(char *s1)
{
char *s2=s1;
int i,j=0;
for (i = 0; i<strlen(s1); i++){
if (s1[i]!=' ') {
s2[j]=s1[i];
}else {
j--;
}
j++;
}
s2[j]=0;
return s2;
}
Is this right???If yes,could you explain me why I have to initialize the pointer *s2 with the first element of the array s1...???If I don't initialize the pointer,or initialize it with something else,I get a segmentation fault...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|