Pointers........

#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
Why what?
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.

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.
closed account (zb0S216C)
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
@Framework, what are you talking about? How does cout << Pointer not work? And he's using printf too..
closed account (zb0S216C)
hamsterman wrote:
How does cout << Pointer not work? (sic)

Should have gone to Specsavers. I never said it wouldn't work.

Wazzak
Topic archived. No new replies allowed.