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.