Why can't you assign an array to another array, but you can use arrays as parameters and arguments?
Lets take this example:
Say this is a function prototype: int readArray(int myArray[]);
And I have this array in main: int theArray[] = {1,2,3};
So finally I call it: readArray(theArray);
The statement "readArray(theArray);" sends the address of theArray[0] to the function readArray and myArray is now pointing at the address of theArray. They both point to the same address.
That works fine, but can anyone tell me why this doesn't work?
In main:
int theArray[];
int myArray[];
myArray = theArray;
My compiler tells me that C++ forbids assignment of arrays. Why is this?
In the first example when I call the function with the array, it is essentialy doing the same thing, setting myArray equal to theArray. So why does this work in function calls but not when explicitly assigning them?
int writeString(constchar* const);
int main()
{
constchar* const str;
readString(str);
}
, but the following doesn't
1 2 3 4 5 6
int main()
{
constchar* const foo = "foo";
constchar* const bar = "bar";
foo = bar;
}
Immutability is something that applies after initialization. Passing arguments counts as initialization of the respective parameters, which may be consequently immutable in the function. Such is the case with aliases (which can not be rebound after initialization), arrays, and constants.