Home > database >  A pointer hold 2 memory location?
A pointer hold 2 memory location?

Time:09-26

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

#include <iostream>

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 pointer &rabbit it will hold the address of rabbit.

  • Related