What happens when I assign a pointer to another pointer?

Related searches

I am facing a problem understanding this code were a pointer points to another pointer. I am required to find the output but I am confused at the line P equals Q, what does that mean? here is the code:

int x;
int y;
int *p = &x;
int *q = &y;
x = 35; y = 46;
p = q;
*p = 78;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;

thanks

The line

p = q

is setting the value stored at p (which is a pointer) to be equal to the value stored in q (also a pointer). Pointers store addresses as their value, so that line is making p and q store the same address now.

So basically you are changing p to point at the address of the integer y.

Continuing from there, the next line:

*p = 78;

is going to the address stored in p and changing the value to be 78.

Pointers: Pointing to the Same Address, There are no constructors involved here. In fact this code is pure C code. int x = 2; // pointerone points to the memory address of x int*� If you assign a pointer to another pointer, then both pointers point to the same value. As you say, in this case, both pointers will point to b, which is 6. 3 0

Means that both pointers now point to y value

p = q;

What is happening when you assign a pointer to another pointer , Now choose another box to contain int *b = &a . int *b is also a variable stored somewhere in memory, and it is a pointer that contains &a� if you also want pointer to point to 0x013797c0 , you'll have to add another pointer = rabbit, but then you'll lose reference to object at address 0x0134a1a8 and will leak memory.

Let's go through line by line: First we are declaring integers x, and y,

int x;
int y;

Then we are creating pointer *p and assigning x's address to it by saying,

int *p = &x;

Then we are creating pointer *q and assigning y's address to it by saying,

int *q = &y;

You must know that pointers points to addresses, and & operator return's operand's address.

Then we are assigning x = 35; y = 46;. You need to know that p still points to x and q still points to y.

Then in line p = q;, we are saying that pointer p will point address that q is pointing to (which is y's address)

In line *p = 78 we are dereferencing pointer p and assigning 78 to it. What does it mean? Well, remember pointer p was pointing y, so we are changing y's value to 78 in this case.

Now x is 35 and y is 78. Also pointers p and q still pointing to y's address.

Line cout << x << " " << y << endl will print 35 78,

and line cout << *p << " " << *q << endl will print 78 78 because remember pointers p and q was pointing to y and dereferencing these pointers will show y's value which is 78.

C++ pointer assignment, The first things to do with pointers are to declare a pointer variable, set it to point We may think of setting a pointer variable to point to another variable as a� Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x. Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer. After pointer assignment, the two pointers are said to be "sharing" the pointee.

I am confused at the line Q equals P, what does that mean?

I am guessing you mean the line "p = q;"

It is trivial to write code to command the compiler to present more information.

The following cout's report both address and value info.


#include <iostream>
using std::cout, std::cerr, std::endl, std::hex, std::dec, std::cin, std::flush; // c++17

#include <iomanip>
using std::setw, std::setfill;

#include <string>
using std::string, std::to_string;

#include <sstream>
using std::stringstream;


int main(int , char** )
{
   int retVal = 0;

   int x;
   int y;

   int *p = &x;  // p points to x
   int *q = &y;  // q points to y

   x = 35; y = 46; // assignment

   // addresses of ---------------------vv----------------vv
   cout << "\n  &x &y: " << setw(16) << &x << setw(16) << &y
   //              ---------------------vv----------------vv--values of
        << "\n   x  y: " << setw(16) <<  x << setw(16) <<  y << endl;

   // addresses of ---------------------vv----------------vv
   cout << "\n  &p &q: " << setw(16) << &p << setw(16) << &q
   //              ---------------------vv----------------vv--values of pointers
        << "\n   p  q: " << setw(16) <<  p << setw(16) <<  q
   //              ---------------------vv----------------vv--values of what pointers point at
        << "\n  *p *q: " << setw(16) << *p << setw(16) << *q << endl;

   cout << "\n  p = q; ... now p and q both point to y";
   p = q;

   cout << "\n  *p = 78 ... now y is 78" << flush;
   *p = 78;
   cout << "\n              and x is unchanged: " << x << endl;

   // addresses of ---------------------vv----------------vv
   cout << "\n  &x &y: " << setw(16) << &x << setw(16) << &y
   //              ---------------------vv----------------vv--values of
        << "\n   x  y: " << setw(16) <<  x << setw(16) <<  y << endl;

   cout << "\n  and both p and q point to y" ;
   // addresses of ---------------------vv----------------vv
   cout << "\n  &p &q: " << setw(16) << &p << setw(16) << &q
   //              ---------------------vv----------------vv--values of pointers
        << "\n   p  q: " << setw(16) <<  p << setw(16) <<  q
   //              ---------------------vv----------------vv--values of what pointers point at
        << "\n  *p *q: " << setw(16) << *p << setw(16) << *q << endl;
   // after assignment, p & q -----------^-----------------^--- point to the same address

   return retVal;
}

Typical output:

  &x &y:   0x7ffeefe1e9bc  0x7ffeefe1e9c0
   x  y:               35              46

  &p &q:   0x7ffeefe1e9c8  0x7ffeefe1e9d0
   p  q:   0x7ffeefe1e9bc  0x7ffeefe1e9c0
  *p *q:               35              46

  p = q; ... now p and q both point to y
  *p = 78 ... now y is 78
              and x is unchanged: 35

  &x &y:   0x7ffeefe1e9bc  0x7ffeefe1e9c0
   x  y:               35              78

  and both p and q point to y
  &p &q:   0x7ffeefe1e9c8  0x7ffeefe1e9d0
   p  q:   0x7ffeefe1e9c0  0x7ffeefe1e9c0
  *p *q:               78              78

As coded, these addresses are all in automatic vars ... no dynamic memory used.

10.1 Basic Pointer Operations, Since we can have pointers to int, and pointers to char, and pointers to any structures come as too much of a surprise that we can have pointers to other pointers. is a ``generic pointer,'' suitable for assigning to or from pointers of any type, the which just happens to be the same node that the main list pointer points to,� It is also legal to assign one pointer to another, provided that they are the same type: int * ptr1, * ptr2; // two pointers of type int ptr1 = ptr2; // can assign one to the other // now they both point to the same place

Chapter 22: Pointers to Pointers, int num = 10; int *ptr = &num; // Assign address of num to ptr // You can also assign a pointer variable to another int *ptr1 = ptr; // Initialize pointer using another pointer How pointers are stored in memory. You got a basic picture of pointer working. Let us take a closer look on how pointer variables are stored in memory.

Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below. A variable that is a pointer to a pointer must be declared as such.

The reason is that, when adding one to a pointer, the pointer is made to point to the following element of the same type, and, therefore, the size in bytes of the type it points to is added to the pointer.

Comments
  • There is no line in your code of the form "Q equals P". Remember assignment is to the left i.e. an expression p = q changes the value of p, and is described in words as "p equals q" or more correctly as "p is assigned the value of q".