1

I am new to c++ and I need help as I am bit confused if the two ways which I thought would be different are actually the same thing. So here is my code of passing down the character array to a function.

     // This is the first style I use
   void functionPTR( char *ptr ) // Pass by reference 
               {
                     // just put up some random code
                 strcpy(ptr,"BY_POINTER"); 
                 cout<<ptr[0]; // random thing
                }

// This is the Second style I use
   void functionVAL( char val[] ) // Pass by value
                 {
                  ... code here
                }

This is the confusion that I have. I always thought that the first one is basically pass by reference and any changes in the variable in function would change the original passed variable and thought that the second is the traditional pass by value way where function creates its own value and uses it to modify which does not affect the original passed variable.

But things here are not as expected by me. In both the cases any change made in the function to the variable is reflected in the original passed variable.
Can anyone please explain me how this thing actually works. Please dont be harsh on me as I am still learning it on my own.

Thanks for stopping by to read my que and help me out.

Kushan Mehta
  • 190
  • 3
  • 11
  • 2
    in this case, an array decays to a pointer of that type, so both are exactly the same. – Hatted Rooster Nov 02 '16 at 17:23
  • 1
    no no no no no no | what bates said is correct. – Ryan Nov 02 '16 at 17:23
  • So basically both do the same thing but are just different ways of representation right? – Kushan Mehta Nov 02 '16 at 17:24
  • 2
    This question has already been answered [here](http://stackoverflow.com/questions/5573310/difference-between-passing-array-and-array-pointer-into-function-in-c). – Matt C Nov 02 '16 at 17:29
  • I completely disagree on this question being marked as a duplicate since it explicitely addresses _C++_, while the other question is asking about _C_. In fact, even the accepted answer refers to a C99 draft. Folks at SO are usually very picky (and correct) about C not being C++, so I don't understand why this should be a dupe. – andreee May 21 '19 at 08:49

1 Answers1

7

They are both exactly the same.

When passed as a function parameter, val decays to a pointer type: it's type too is char*.

In neither case is a deep copy of any array taken.

Whether you use val[i], ptr[i], *(val + i), or *(ptr + i) to access array elements is purely a matter of personal taste.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    @KushanMehta copy of the variable is created in both cases, variable is a pointer, which is copied, not data where it points to. – Slava Nov 02 '16 at 17:27
  • Oh this helped me solve my doubt entirely. Thanks a ton. Will accept your answer in 9 minutes (time limit). – Kushan Mehta Nov 02 '16 at 17:28
  • 1
    @KushanMehta you're correct, the variables aren't copied. Instead, a single pointer is passed to the function which points to the array. So in either function you're working with the **very same** array. – Matt C Nov 02 '16 at 17:28
  • @Slava I got that thing. So is there really a difference in memory between pointer and original char array. Thanks – Kushan Mehta Nov 02 '16 at 17:29
  • @KushanMehta your question does not make much sense. Array is an area of memory, pointer is an address. In C/C++ array name can decay to the pointer of the first element, but array and a pointer are different things. – Slava Nov 02 '16 at 17:32
  • Yes you are correct . sorry didnt keep that in mind. Forgot about the concept of First pocket pointer :-) Thanks – Kushan Mehta Nov 02 '16 at 17:33