How does strcpy() change what is at str though, I had people explaining it to me on IRC before but I still don't really understand it.
First, you have to realize that when you send an argument to a function, the parameter variable is created and then the argument is assigned to the parameter variable. In other words, the argument is stored in the parameter variable.
Second, whenever you pass the name of an array to a function, the address of the array is actually what is passed to the function.
Third, whenever the compiler encounters a string literal(i.e. anything between double quotes), like "World!", it slaps a '\0' on the end of it, stores it in memory somewhere, and returns the address. Additionally, the string is const, so you cannot change it. For instance, this doesn't work:
Code:
char* str = "Hello";
str[0] = 'a';
It will compile without error, but your program will crash. Digressing a bit further, this does work:
Code:
char str[] = "Hello";
str[0] = 'a';
That's because the first line causes an internal strcpy() to be performed. As before, the compiler takes the string literal "Hello" and slaps a '\0' onto the end and stores it in memory somewhere, but in this case the compiler strcpy()'s the string to the address of the newly created char array str. Furthermore, the copy is not const like the original, and it can be modified. Note: there are two copies of "Hello" in memory.
In the example in the post you are asking about, str is an array and it is passed to the function. Therefore, the address of str is passed to the function, and it gets stored in the variable param. strcpy() is a function that says to copy one string into another string. In particular, strcpy(param, "World!") says to copy "World!" to the address stored in param. The address stored in param is the address of str, so "World!" gets copied to str's address. After the copying is done, there are two copies of "World!" in memory: one is located at str, and another is located elsewhere in memory(note: the string located at str is modifiable, while the other string is const and can't be modified).
I don't see why strcpy(param, "World!");
is different from param = "World!";
The statement:
is executed like this: first, the compiler treats all string literals the same, so it slaps a '\0' onto the end of "World!" and stores it in memory. Then the compiler returns the address of where it stored the string, which then gets assigned to param. In C++, when you assign a value to a variable, it overwrites what was previously assigned to that variable, e.g.:
Code:
int i = 10;
i = 20;
cout<<i<<endl;
Prior to the assignment, param contained the address of str. After the assignment param contains the address of "World!". str is wholly unaffected by what is assigned to param. Another way of looking at it is: before the assignment param and str pointed to the same place in memory. So, if you use param to change what is stored at that address, then you will change what str points to. That's what strcpy() does. However, assigning a different address to param does nothing to str, it just makes param point elsewhere. Here is a diagram of what is happening:
Code:
"Hello\0"
/ \
/ \
/ \
str param
strcpy(param, "World!");
"World!\0" (const)
"World!\0" (modifiable)
/ \
/ \
/ \
str param
Code:
"Hello\0"
/ \
/ \
/ \
str param
param = "World!";
"Hello\0" "World!\0" (const)
/ /
/ /
/ /
str param