Sep 4, 2011 at 3:25pm UTC
#include<stdio.h>
#include<conio.h>
int main()
{
int k=35,*z,*y;
clrscr();
z=&k;
y=z;
*z++ = *y++;
k++;
printf("\n\nk = %d , z = %d , y = %d",k,z,y);
getch();
return 0;
}
output = k=36 y = 2293624 z = 2293624
Why output looks like above mention please explain...???
Last edited on Sep 4, 2011 at 4:06pm UTC
Sep 4, 2011 at 4:07pm UTC
Let's go through the lifespan of each, shall we?
k= 35. K is incremented by one. It is then printed out.
(z and y are kept the same throughout the program)
y is given the adress of k , then the adress is incremented.
I'm a begginner too, so I really have not got a clue as to what you did with the output.
Sep 4, 2011 at 4:21pm UTC
2293624 is the address that both y and z point to (the address of k) which is what you printed out.
De-reference both y and z in the call to printf to print the value of k instead.
Sep 4, 2011 at 5:03pm UTC
You need to dereference
z and
y to obtain the value of the variable they're pointing to. If you want to print the address of the variable pointed to by a pointer, you would do this:
1 2 3 4 5
// Style 1:
std::cout << *( &Pointer ) << std::endl;
// Style 2:
std::cout << *&Pointer << std::endl;
Either way works.
Wazzak
Last edited on Sep 4, 2011 at 5:08pm UTC
Sep 4, 2011 at 5:26pm UTC
@Framework, what are you talking about? How does cout << Pointer
not work? And he's using printf too..