VansFannel
VansFannel

Reputation: 45921

Pointers and reference operator (&)

I'm learning C++ and I have found this on a book:

#include <iostream>
using namespace std;

int main()
{
int Age = 30;
int* pInteger = &Age; // pointer to an int, initialized to &Age

// Displaying the value of pointer
cout << “Integer Age is at: 0x” << hex << pInteger << endl;

return 0;
}

The book said that the output is the address in memory where Age is stored.

But the book doesn't talk about this:

*pInteger = &Age;
 pInteger = &Age;

What is the difference between these two assignments?

Upvotes: 0

Views: 874

Answers (5)

Oleg Titov
Oleg Titov

Reputation: 1140

First we declare and initialize a variable of type int*

      int*     pInteger   =          &Age;
//| var type | var name |   | value of type "int*"  |

Next we try to use assignment:

          pInteger       =         &Age;
//| var of type "int*" |   | value of type "int*"  |
         *pInteger      =         &Age;
//| var of type "int" |   | value of type "int*"  |

The last is incorrect until we cast int* to int (If our goal is to use adress like integer value). Unary operator dereference operator * means that we need something located at specified adress.

Upvotes: 1

djna
djna

Reputation: 55907

It would probably be easier to understand if you break this

 int* pInteger = &Age;

down into two steps

 int* pInteger;

delares a variable of type pointer to int, and then

 pInteger = &Age;

assign the address of Age to pInteger, so pInteger now points to a specific integer.

If you wrote

 *pInteger = 95;

you would assign a new value to whatever pInteger is currently pointing to.

When you write

 int* pInteger = &Age;

you are declaring the variable and giving it an initial value, so that's not the same as the apparently similar

  *pInteger = &Age;

which is using pInteger but not declaring it so here you get the address of Age and are effectively attempting to assign that to the thing which pInteger points to, assigning an address to an int not being a good thing to do.

Upvotes: 3

CashCow
CashCow

Reputation: 31435

You seem to be confused by this line

int* pInteger = &Age; // pointer to an int, initialized to &Age

The * symbol here is declaring pInteger as a pointer to an int. This is initialised (not assigned) to the address of Age which is permitted because Age is an int.

You could type

*pInteger = 45;

and that would be assigning to the integer to which pInteger points.

You can also type

int y = 35; 
pInteger = &y;

which would be reassigning the pointer to point to a different place.

Upvotes: 6

Masked Man
Masked Man

Reputation: 11025

pInteger is a pointer to int, and it is initialized to the address of Age in the first case (assuming you meant int*pInteger = &Age;), and assigned in the second case.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258598

int* pInteger = &Age; is not an assignment, but an initialization. Here, * is part of the type - int*, not to the integer *pInteger, like below.

The two you ask about are assignments:

*pInteger = &Age;
 pInteger = &Age;

The difference is that the first is illegal, the second is OK. That's because &Age has type int*, whereas *pInteger has type int, and it's illegal to assign an int* to an int. pInteger, however, has type int*, so the assignment is OK.

Upvotes: 2

Related Questions