First, try the following modification of your function to see that the array is in fact "passed by reference":
Code:
void changeMe(char param[])
{
param[0] = 'X';
}
As to why, your function:
Code:
void changeMe(char param[])
{
param = "World!";
}
doesn't work they way you expected, you have to understand what "passing by reference" really means. When you really understand pointers and passing by reference, you will understand the truth of tretton's statement:
Remember that everything is passed by value
The programming books all lie when they say some arguments are "passed by reference" and others are "passed by value"--because all arguments are really passed by value in that they are all copied.
If you pass an int to a function, then the int is copied for the function and the function operates on the copy. So any changes the function makes to the copy do not affect the original int back in main(). When you pass a pointer to a function, the pointer is copied as well. However, a pointer is an address and a copy of an address refers to the same location in memory as the original address. So when you pass a pointer to a function, the function can use the copy of the address to change the value at that location in memory. Try this code:
Code:
#include <iostream>
using namespace std;
void changeIt(int* p)
{
cout<<p<<endl; //006BFDF4
}
int main()
{
int num = 10;
int* ptr = #
cout<<ptr<<endl; //006BFDF4
changeIt(ptr);
return 0;
}
Since the address stored in ptr in main() is the same address stored in p in changeIt(), the changeIt() function can use p to change the value at that location:
Code:
#include <iostream>
using namespace std;
void changeIt(int* p)
{
cout<<p<<endl; //006BFDF4
*p = 25;
}
int main()
{
int num = 10;
int* ptr = #
cout<<ptr<<endl; //006BFDF4
changeIt(ptr);
cout<<num<<endl; //25
return 0;
}
It is also important to note that this is pass by value in the full sense--just like when passing an int variable by value to a function, and the function is unable to change the value of the variable back in main(). While the function above is able change the value that ptr points to, the function cannot change ptr itself back in main(), i.e. the function cannot assign a different address to ptr. Here is an example:
Code:
#include <iostream>
using namespace std;
void changeIt(int* p, int& anotherNum)
{
p = &anotherNum;
cout<<*p<<endl; //32
}
int main()
{
int num = 10;
int* ptr = #
int num2 = 32;
changeIt(ptr, num2);
cout<<*ptr<<endl; //10
return 0;
}
That is similar to what is happening in your function. This statement:
Code:
#include <iostream>
using namespace std;
void changeMe(char[]);
int main(void)
{
char str[] = "Hello ";
cout << str;
changeMe(str);
cout << str << endl;
}
void changeMe(char param[])
{
param = "World!";
}
says to assign the address of the string "World!" to param. param is a copy of str and anything you assign it has no effect on str back in main(). In fact, an array name is const, which means it cannot be made to refer to any other location in memory, and this produces an error:
Code:
char str[] = "Hello ";
str = "World!"; //compiler error
so param and str cannot be the same thing or you would get a compiler error.
In conclusion, this function:
Code:
void changeMe(char param[])
{
param[0] = 'X';
}
uses the address stored in param to change the value at that address in memory. Since str back in main() also points to that value, it can see the changes. Both str and param point to the same value, so when one of them is used to change the value, the other one can see that change.
And this function:
Code:
void changeMe(char param[])
{
param = "World!";
}
assigns a new address to param, overwriting the address currently stored in param, which has no effect on the address stored in str back in main(). Subsequently, param and str point to two different values.