Reputation: 64
I have been trying to understand the way pointers are passed as arguments. I have seen in my course book that pointers and arrays are by default passed as reference but the following program gave me unexpected result.
I expected:
Second&First
Second*First
But the output is:
Second&First
First*Second
Is my concept wrong or is there any special trick in this program. Please clear the concept of pointers.
void Alter(char* X, char* Y){
char* T;
cout<<"&S1="<<&X<<"&S2="<<&Y<<endl;
T=X;
X=Y;
Y=T;
cout<<X<<"&"<<Y<<endl;}
void main(){
char X[]="First", Y[]="Second";
cout<<"&x="<<&X<<"&y="<<&Y<<endl;
Alter(X,Y);
cout<<"&x="<<&X<<"&y="<<&Y<<endl;
cout<<X<<"*"<<Y;getch();}
Upvotes: 0
Views: 454
Reputation: 36597
Yes, your concept is completely wrong.
In your Alter()
function, which I simplify here (to remove extraneous output unrelated to your question)
void Alter(char* X, char* Y)
{
char* T;
T = X;
X = Y;
Y = T;
cout<<X<<"&"<<Y<<endl;
}
X
and Y
are both pointers and they are passed by value. So the assignments X = Y
and Y = T
are not visible to the caller i.e. to main()
. Specifically, the assignments in the function (X = Y
and Y = T
) have no effect that is visible to main()
.
However, X
and Y
each point to (or can be used to refer to) something - in your code, the first character of the arrays in main()
. Those somethings, in the function, are *X
and *Y
. One archaic description - which seems to be what your course book is using - is that they are the things "passed by reference".
If we were to change Alter()
to make assignments to *X
and *Y
viz;
void Alter(char* X, char* Y)
{
char T; // note the missing * here relative to your code
T = *X; // note the added * in this line
*X = *Y; // note the added *s in this line and the next
*Y = *T;
cout<<X<<"&"<<Y<<endl; // this statement is unchanged
}
In this case, the output would be
Sirst&Fecond
Sirst&Fecond
(i.e. the first characters of the arrays swapped, and that effect is visible to main()
).
We could also change Alter()
to
void Alter(char*& X, char*& Y) // note the ampersands here
{
char* T;
T = X;
X = Y;
Y = T;
cout<<X<<"&"<<Y<<endl;
}
In C++, the ampersands here mean that X
and Y
are references to pointers. So the assignments in this function would be visible to the caller.
However, your example main()
(which again I simply to remove extraneous output) will not compile with using this function
int main() // main() returns int in standard C++, not void
{
char X[]="First", Y[]="Second";
Alter(X,Y);
cout<<X<<"*"<<Y;
}
This will not compile because X
and Y
in main()
are arrays, and arrays ARE NOT pointers - so they cannot be passed to a function that expects to be passed references to pointers. This means that your function Alter()
cannot be used to swap the arrays in main()
.
Upvotes: 2
Reputation: 3427
The output is correct. Your expectation is somewhat different from the actual behaviour.
Whenever it comes to pointers, always treat them as variables that contain some memory address.
See step by step, what is happening.
1) char X[]="First", Y[]="Second";
----------------
| F | <--- X
----------------
...
----------------
| S | <--- Y
----------------
2) cout<<"&x="<<&X<<"&y="<<&Y<<endl;
Addresses of pointers X
and Y
are outputted onto the screen.
3) Alter(X,Y);
On execution of this function call, pointers X and Y are copied to the parameters of the Alter
function, say, X' and Y'
----------------
| F | <--- X, X'
----------------
...
----------------
| S | <--- Y, Y'
----------------
4) T=X; X=Y; Y=T;
On execution of this code, X' and Y' pointers are swapped.
----------------
| F | <--- X, Y'
----------------
...
----------------
| S | <--- Y, X'
----------------
5) cout<<X<<"&"<<Y<<endl;
As you can see in Step 4, Because X' points to memory location containing 'S', 'Second' is printed first and 'First' is printed later.
6) cout<<"&x="<<&X<<"&y="<<&Y<<endl;
Before execution of this instruction, the parameters of Alter
function have become out of scope.
So, Now, the scenario is like this:
----------------
| F | <--- X
----------------
...
----------------
| S | <--- Y
----------------
7) cout<<X<<"*"<<Y;
As you can see in Step 6, Because X points to memory location containing 'F', 'First' is printed first and 'Second' is printed later.
Upvotes: 2
Reputation: 3764
A pointer is like any other integer you pass to a function, except that the integer has a meaning for the compiler - it is a defined location in memory.
So pointers themselves are passed by value. The content they point to is said to be passed by reference.
When you modify a pointer variable passed to a function, the actual value of the pointer in the calling function remains unchanged. However, when you modify the value pointed to by the pointer, the value changes everywhere.
In your example, X and Y are pointers; they contain the starting addresses of "First" and "Second". "First" and "Second" is the data pointed to by X and Y respectively.
Consider the statements derived from your program, only using printf
instead of cout
:
char X[] = "First";
// Prints "First"
printf("%s", X);
// Prints address of 'F' in memory
printf("%x", X);
// Prints address of X - the location where the value printed above is stored
printf("%x", &X);
Upvotes: 1