I have the following scenario:
Expand|Select|Wrap|Line Numbers
- 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? -->
Expand|Select|Wrap|Line Numbers
- 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