Stefan
Stefan

Reputation: 113

C++ difference between address

I have the following structure for a node in a linked list.

struct node {
    int info;
    nod *next;
};

I have declared p a pointer to a structure (node) and initialized it.

node *p = new node;

I don't understand the difference between p and &p, they are 2 addresses but shouldn't them have the same address?

cout<<p<<" "<<&p;

Upvotes: 2

Views: 636

Answers (5)

molbdnilo
molbdnilo

Reputation: 66371

Consider this magnificent artist's rendition of a piece of hypothetical memory immediately after executing node *p = new node;:

    4     5     6     7     8     ....      
...___________________________________ ...
...    |     |     |     |     |     | ...
...    |     |  8  |     | info next | ...
...    |     |     |     |     |     | ...
...----------------------------------- ...
                ^
                |
               "p"

There is a node starting at cell number 8, and cell number 6 (which we call "p") contains the number 8.

In other words,
The value of p is 8, which is the address of the node object, while
the value of &p - the address of the variable named "p" - is 6.

Upvotes: 1

Raydel Miranda
Raydel Miranda

Reputation: 14360

I think Borgleader and chris have the best ideas for explain this, in the simplest way.

First you have to remember a pointer is also a variable. So, while an int variable holds an integer, a pointer holds an address. And like other variables it has an address for itself.

Hence when you do:

cout << p ...

you're printing the value of p (the address of the new node you have just created) on the other hand

cout << &p ...

will print the address of p. There are two completely different address.

Upvotes: 2

Galik
Galik

Reputation: 48625

Let's take a look at this a little differently:

struct node {
    int info;
    node *next;
};

node n; // the node itself

node* p = &n; // p is a "node pointer" (node*)
              // its value holds the address of (node) n

node** pp = &p; // pp is a pointer to a "node pointer" (node**)
                // its value holds the address of (node*) p

Does that make it a little clearer?

Upvotes: 4

ravi
ravi

Reputation: 10733

So, let's have a simple example to elaborate this:-

int x = 4;

Through this 4 would be stored in a memory created to the size of int. That chunk of memory is named as variable x in your program. Let's say that memory address is ADDRESS_1.

int *ptr = &x;

Through this statement another chunk of memory is created to the size of pointer ( same for any pointer type whether float,string or some user-defined type).Let's say that memory address is ADDRESS_2. That chunk of memory would hold the address of x i.e ADDRESS_1. This is all what pointer means.

And when you do

&ptr;

This means you are referring to address of ptr which is ADDRESS_2 in this case.

It would give you the address where ptr is stored in memory.

Hope things would be all clear for you as far as definition of pointer goes...

Upvotes: 1

Etixpp
Etixpp

Reputation: 328

In C++ you use & before a poiner if you want to write a variable to him or better said: let them pointer point on a adress cause & gives the adress of the var.

int i = 0;
int* pointer = &i;

If you want to use the contant of the variable the pointer points to you deference it with *.

int y = *i;

basicly p is the pointer of the node and &p is the adress of the pointer of the node.

p has its own adress, at exampel if you want a other pointer to point on p which points on the node you do foo secondpointer = &p

Upvotes: -2

Related Questions