Simple Pointer Question

I don't understand why the out put is: 98 98
98 98
When was x and y ever initialized?

Code is:

int x;
int y;
int *p = &x;
int *q = 98;
int*p = *q;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
Sorry, output is

98 98
98 98
what are you trying to get your output to be?
The output is
98 98
98 98
I just don't understand why. It's an example from my book
Last edited on
closed account (D80DSL3A)
That code would not compile.
It's probably something like this:
1
2
3
4
5
6
7
8
int x;
int y;
int *p = &x;
int *q = &y;
*q = 98;
*p = *q;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl; 
It's true the code didn't compile. Either it was a simple typing error, or the book is pretty bad.
Topic archived. No new replies allowed.