I'm teaching pointers to myself through my book and I'm stuck on one of the practice problems. Here it is:

//What is the output of the following C++ code?
int x;
int y;
int *p = &x;
int *q = &y;
*p = 35;
*q = 98;
*p = *q;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;

I know the answer is 98 98 98 98 but I don't understand how the variables x and y can get 98. Even the first assignment of *p = &x, what does that even mean? Doesn't that mean the memory address of x is assigned to pointer variable p? If someone could run me through this code I would very appreciate it...my head is starting to hurt.

Thanks.

set pointer p to reference address of x
set pointer q reference address of y
-when we said address we are not actually refering the its value but the location in the memory, so when we

set pointer p value = 35,
set pointer q value = 98,

then set pointer p value to pointer q value (98)
when we print x, we are are getting the value 98 since the address of x (&x) value has been set to the pointer p value which is 98.

sorry for my english but i hope you get the point.

int *p = &x;

You can break this statement as

int *p;              // Define a pointer to an integer (p is a variable that
                           // is capable of holding address of an integer variable
p = &x;                    // & can be said to be  'address of' operator. hence
                           // assign to p address of variable x

Now whenever you say, *p , it means de-referencing p. That means , *p gives you value of variable at address contained in p i.e. nothing but value of x

I think I figured it out (I THINK). So looking back at my original code, does line 4 store the address of the variable 'x' to the pointer variable 'p' and does line 6 store the int value 35 to the memory location of variable 'x' (meaning the value of pointer variable '*p' (&x) is not being replaced with 35)? If I think of it like that then it makes sense since both lines 4 and 6 are not essentialy "re-assigning" the variable '*p'. I guess I thought the value of '&x' in the pointer variable '*p' was being replaced by 35 because of line 6, but I'm guessing that's not true?

Can someone verify this?

Wait, no, nevermind. Disregard my first post. I realized what I said was utterly nonsense. The pointer variable '*p' does get reassigned to the value of the pointer variable '*q' (98). So essentially both pointer variables (*p, *q) hold the value 98.

Because the address of the variable 'x' and 'y' are held in pointer variables '*p' and '*q' respectively, when the system calls to output 'x' it looks for the location of 'x' (&x), which is stored in '*p' and then outputs the value of where that location is: value of '*p' (98). And the same with 'y'. Correct?

You have understood the concept , i guess . But the way u have put it is a little ...ummm....

The pointer variable '*p' does get reassigned to the value of the pointer variable '*q' (98). So essentially both pointer variables (*p, *q) hold the value 98.

This is OK..

Now,

Because the address of the variable 'x' and 'y' are held in pointer variables '*p' and '*q' respectively, when the system calls to output 'x' it looks for the location of 'x' (&x), which is stored in '*p' and then outputs the value of where that location is: value of '*p' (98). And the same with 'y'. Correct?

I would put it something like this,
Whenever you say *p = some_value , what is done here is set some_value to the value of location pointed by p

Thus , when you do *p = 35; , it is same as x = 35
when you do *p = *q , you are doing nothing but x = y

i am sorry i got lost let me try simplify it:
Pointer variable holds address ok?
EX:

int x=0;//x Value=0,             x Address in memory EX:(32Bit)0016F8C8 
	int y=1;//y Value=1,             y Address in memory EX:(32Bit)0016F8BC 
	int*p=&x;//p Value=0016F8C8      p Address in memory EX:(32Bit)0031F9F0
                  //p Value is (Addressof x)
	cout<<&x<<" " <<&y<<" " <<&p<<endl;
	cout<<x<<" " <<y<<" " <<p<<endl;
         // As x=0 changes the value 
         p=&y;//Changes the p value to the address of y
        //when you want to access the y value from p
        *p=6;//(*) means go to the address in p and change its value to 6

i hope i made it some how clear

Look at it this way...

X is a variable right... P is a pointer that points at X (p = &x)... * means pointer and & means reference (http://www.cprogramming.com/tutorial/lesson6.html)

now think of P as kind of a portal to access X... anything you enter into P actually gets set in X, and setting X also changes P because its like looking through a portal at the same thing...

When sending variables to a function, it is much better to send a pointer or a reference because you can then in the function change the value of the variable and when you get back to the previous method the value is still changed... This is also VERY resource effecient

alternatively sending a variable without a pointer or reference indication creates a second variable with the same value and uses more resources

Coding Example:

void MyClass::ParentMethod(){

    int var = 0;
    
    changeVar(*var);

    cout<<var<<endl; //outputs "4"
}

void MyClass::changeVar(int& var){

    var = 4;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.