Hi all,
I have the following scenario:
Now my problem is that the length of the string doubles after the operation is over. So how do I re-size the memory pointed to by 'b' to accommodate the change in size of the string?
I cannot have a local array of twice the size of 'b' and return that because that would be returning a local pointer.
Will this be correct and work fine? -->
Please if somebody could help :((
-
-Sid
I have the following scenario:
Code:
int main()
{
char a[] = "Hello";
printf("Modified string: %s\n", modify_str(a));
}
char *modify_str(char *b)
{
/* Insert a '+' after every letter in the string */
/* "Hello" becomes "H+e+l+l+o+" */
return b;
}
I cannot have a local array of twice the size of 'b' and return that because that would be returning a local pointer.
Will this be correct and work fine? -->
Code:
char *modify_str(char *b)
{
char *local;
local = (char *)malloc(strlen(b)*2 + 1);
/* 'local' gets filled up with the modified 'b' */
b = local; /* Is this wrong? Why is this wrong?? */
return b;
}
-
-Sid
Comment