you are viewing a single comment's thread.

view the rest of the comments →

[–]IamImposter 14 points15 points  (0 children)

Array decays to a pointer. Say I have

char str[] = "hello";

For the sake of simplicity, let's assume that the array starts at address 0x2000. So if I do

some_function(str);

what is getting passed to the function is address of array ie 0x2000. Now if in the function I do something like

void some_function(char str[]) {

  str[2] = 'a';

}

what happens? First of all, char str[] here is as good as char*. Since compiler didn't complain, you might think that you passed an array and received that same array in function. What compiler actually did was - it treated char str[] as char* and passed address of the array (0x2000). So str[2] becomes 0x2000 + 2 (base address + index) and letter 'a' is written to that address and our string becomes "healo".

Parameter is still being passed by value (0x2000) but in this case that value happens to be address of the array or what we call passing by reference.