0

I'm new to C++ and currently learning about pointers. This was from my notes:

#include <iostream>
using namespace std;
    
int main()
{
    int value1 =5, value2 = 15;
    int *p1, *p2;
    
    p1 = &value1; //why does it mean by the & here?
    p2 = &value2; //and here?
    *p1 = 10;
    *p2 = *p1;
    p1 = p2;
    *p1 = 20;
    
    cout << "Value1 = " << value1 << "Value 2 = " << value2;
    return 0;
}

What does the & mean here?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

& means different things in different contexts. But in this particular context, it is the address-of operator, which returns the memory address of the object specified to its right. So, in this case, p1 is being assigned (points to) the memory address of value1, and p2 is being assigned (points to) the memory address of value2.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770