I want to call a function and I want that function to change the contents of a string or array in the program to a constant.
Psuedocode:
some_array = "hello"
print some_array #prints "hello"
changeArray(some_array)
print some_array #prints "bingo"
I know I have to pass the pointer to that function. Here's what I wrote,
void changeArray(char *arr){
arr = "bingo";
}
int main(int argc, const char* argv[]){
char *blah = "hello";
printf("Array is %s\n",blah);
changeArray(blah);
printf("Array is %s\n",blah);
return EXIT_SUCCESS;
}
How can I do this?
You are passing the pointer to the array by value instead of by reference. It should be:
void changeArray(char **arr){
*arr = "bingo";
}
int main(int argc, const char* argv[]){
char *blah = "hello";
printf("Array is %s\n",blah);
changeArray(&blah);
printf("Array is %s\n",blah);
return EXIT_SUCCESS;
}
You gave the the address of "hello" to changeArray
function, but in the function you changed the value passed, not the original pointer. The change I made passes the address of the pointer, and the pointer itself is changed in the function.
Please not char *blah = "hello";
defines a pointer to a constant string, as well as *arr = "bingo";
, this is both fine, but if you consider to change the string itself, you will not be able to.
EDIT:
When you pass an argument, even a pointer, to a function, you actually copies it to some place where the function read it from there (usually the stack). You don't pass the argument itself, but a copy of it. When the function modifies it (like in arr = "bingo";
) it modifies the copy of the variable, not the original variable. So in order to change the variable itself, we pass the address of the variable to the function (changeArray(&blah);
- the &
means address of-
) and in the function we modify the variable stored in the address we passed (*arr = "bingo";
- the *
means the variable in the address arr
).
Assuming the original blah
pointer is located in the address 0x00000000 and contains the address of "hello"
string which is for example 0x00000010. if you pass blah
to the function, you copy it to a new variable, arr
, which is located in address 0x00000020 for example
Variable Address content
-------------------------------
blah 00000000 00000010 (points to hello)
"hello" 00000010 "hello" (this is just an example, so don't be hard on me :) )
arr 00000020 00000010
"bingo" 00000030 "bingo" (and again...)
now if you change the content of arr
you change the value in address 0x00000020, but not the value in address 0x000000000, so blah
still contains 00000010.
Variable Address content
-------------------------------
blah 00000000 00000010 (points to hello)
"hello" 00000010 "hello" (this is just an example, so don't be hard on me :) )
arr 00000020 00000030 (points to "bingo")
"bingo" 00000030 "bingo" (and again...)
Instead what we do is copy the address of blah
, which is 0x00000000, to arr
and in the function we say - "the content of arr is an address, go to this address and change its content to point to "bingo" string". so now the content in address 0x00000000 (which is blah
) is pointing to "bingo".
Variable Address content
-------------------------------
blah 00000000 00000030 (points to "bingo")
"hello" 00000010 "hello" (this is just an example, so don't be hard on me :) )
arr 00000020 00000000 (points to `blah`)
"bingo" 00000030 "bingo" (and again...)
Hope I didn't confuse you...
There are no arrays in your code. If you are actually trying to modify what your blah character pointer points to, then you need to pass a pointer to a pointer into the function. However, if you want to do this using arrays then you need to do something like:
void changeArray(char arr[]) {
// or you can use char *arr, it's the same thing from
// the C compiler's point of view
strcpy(arr, "blah");
// alternatively, you could set each element. i.e. arr[0] = 'b';
}
int main (int argc, char** argv) {
char blah[100] = "hello"; // this is an array
printf("Array is %s\n", blah);
changeArray(blah); // array decays to pointer
printf("Array is %s\n", blah);
return EXIT_SUCCESS;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With