C++ Array vs Object - Pass by value

You can not pass an array by "pass by value", whereas objects can be passed by value. Here is a snippet:

You can pass an array to a function in only following manner:
int arr[2] = {0,1};
void s(int arr[]){ //This statement is equivalent to: void s(int *arr)
arr[0]=2;
}

s(arr); // This passes array pointer by value and hence the complete is passed by reference. Meaning to say function s will operate on real array and not on a "copied" array of the one passed in argument.


You can pass an object to a function in two ways:
void s(Obj* o) // this statement is equivalent to: void s(Obj & o). Pass by reference
void s(Obj o) // Pass by value. The actual ojbect passed as parameter when invoking the function, gets copied and function acts on copied object and not on actual object

Comments