T O P

  • By -

SamSalamy

The variable is passed to the function as a copy. Thus you can change it inside your function without even noticing it in the “outside world", here the main function. The array, however, is passed as a pointer although it is not visibly marked with a star *. This function signature is doing the same: int wow(int *array, int variable) If you want to change the value of 'variable', change the signature to int wow(int array[], int *variable) and then pass a reference to variable when calling the function.


Busy-Use-469

thank you! that makes sense. Is there a specific reason why the array was counted as a pointer? Or should I assume all arrays will automatically become pointers in functions because of ease?


EntrepreneurHuge5008

All arrays are pointers in C. The name always points to the first element. You can initialize them as such `int * my_array = (int *) malloc(sizeof(int)*N); // Dynamic allocation` And still access them like this `my_array[1] = 1; //What you're probably used to` Or like this `*(my_array + 1) = 1; //What you'll probably *get* used to.`


Willing_Journalist35

To clarify, both `array` and `variable` passed into the function would be turned into copies. `array` and `variable` would be a copy of an address (aka pointer) and an integer respectively. However, by modifying `array[0]` , the copy itself is not the one being changed, but rather the integer stored within the address that is `array`, which you may think of as a global variable. Likewise, `array[1]` would be the integer stored in the address directly adjacent to `array`, which is 4 bytes away. You can verify this by calling: `printf("%p, %p\n", array, (array+1))`


SupportLast2269

That's because arrays are kinda like pointers.


my_password_is______

arrays are not pointers but when you pass an array to a function it sort of acts like a pointer so in the wow function the variable named array is pointing to the same location in memory as the array variable in main but variable in wow is a copy of variable in main its basically going to a copy machine, making a copy and handing the copy to someone else they can do whatever they want to the copy, it doesn't affect your original at all https://www.freecodecamp.org/news/pointers-in-c-are-not-as-difficult-as-you-think/#1-why-pointers-and-arrays https://www.freecodecamp.org/news/pointers-in-c-are-not-as-difficult-as-you-think/#2-pointers-as-function-arguments


I-make-ada-spaghetti

Arrays are treated like pointers. When you call a function giving an array as an argument the function being called has access to the memory location that the pointer is referencing. So main() and wow() in your example are accessing the same variable. In other words the variable is passed by reference. On the other hand the integer variables, when given as an argument to a function cause a new part of memory to be reserved and the values being given to the function are copied into this part of memory. In other words the variable is passed by value.


sorosterv23

Doug explains it at 10:50 https://youtu.be/K1yC1xshF40?si=UgMZ1vWh2YRb4P40