Passing an Array by reference in C

后端 未结 5 484
渐次进展
渐次进展 2020-12-01 09:34

I\'m new to C and I have a doubt.

Since C functions create local copies of it\'s arguments, I\'m wondering why the following code works as expected:



        
5条回答
  •  心在旅途
    2020-12-01 10:07

    In the first code, you are passing the address of the array pointing to the top element in the array. So, when you modify the value in the function and return to the main function you are still accessing the same array which is in the same address. This is called pass by reference.

    However, in the second case, the value of the integer is copied from the main function to the called function. In other words, the two integers are in different address in the memory. So modifying one does not modify the other.

提交回复
热议问题