Home > Blockchain >  A pointer holding 2 memory locations?
A pointer holding 2 memory locations?

Time:09-26

Why does a pointer hold two memory locations? What is the use case for the 2nd memory location?

#include <iostream>

using std::cout;

int main()
{
    string animal;
    string *rabbit = &animal;
    
    cout << rabbit  << " 1st memory location \n" 
         << &rabbit << " 2nd memory location";

    return 0;
}

CodePudding user response:

A pointer holds one memory address. &rabbit is the address of the rabbit variable itself.

CodePudding user response:

enter image description here

As the above image shows pointer p is holding the memory address of another variable which has a value of 50.

eg.

cout<<p; //fff4 address of number 
cout<<*p; //value at location fff4 will be printed that is 50
cout<<&p; //address of pointer p which is aaa3 will be printed

learn more about pointers

CodePudding user response:

As when we declare pointer rabbit it will hold some space in the memory. And when we call &rabbit it will hold the address of rabbit.

  • Related