How do I change the actual argument passed to a function in C?
I want to change the actual argument passed to the function, not a copy of it. For example:
char str[] = "This is a string";
I want to create a function after the call for which the value str
is different. I tried to create a function that takes char**
as an argument, but I just couldn't get what I want.
source to share
Pass char*
in if you want to change the actual line:
foo(str);
...
void foo(char *some_string) {
some_string[0] = 'A';
}
str
now hold "Ahis is a string"
If instead of str
being an array, you had: char *str = "Hello";
and wanted to change where you str
pointed out, then you must pass char**
:
bar(&str);
...
void bar(char **ptr_string) {
*ptr_string = "Bye";
}
str
now points to "Bye"
.
source to share