You will need to pass the address of the string pointer. Since your "string" is really a pointer to char
that makes the thing you want a "pointer to pointer to char", which is written char **
. When you pass it to foo()
you need to pass the address of the pointer, so foo(&string)
is correct.
When you reference the string in the function you you will need and "extra" dereference, for example:
int n = strlen(*s);
Which also applies to the reallocation:
*s = realloc(...);
Alternatively, you could pass it as usual and return the (possibly new) pointer as the value of the function. For example:
char * foo(char *s)
{
if (...)
s = realloc(...);
...
return s;
}
Of course that means you have to call/use the function like so in main()
:
string = foo(string);