Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address of a variable using pointer

Tags:

Consider the following code in C

int x=100; int*addr=&x; 

I know that addr will store the address of x.A question that keeps popping in my mind is that the addr pointer will have its own address and that can again be accessed using ampersand operator and that too will be stored somewhere,so we have here infinite recursion on addresses so where does this end?

like image 313
Naseer Avatar asked Feb 13 '14 10:02

Naseer


People also ask

What is the address of a pointer variable?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.

How do you assign the address of a variable to a pointer?

It is called dereferencing or indirection). To assign an address of a variable into a pointer, you need to use the address-of operator & (e.g., pNumber = &number ). On the other hand, referencing and dereferencing are done on the references implicitly.

How do you get the address of a variable?

To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable: By using "address of" (&) operator. By using pointer variable.

What is the address of a pointer in C++?

In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type.


2 Answers

The address of addr isn't "stored" anywhere explicitly, it just is. If you were to declare a second variable and use it to store that address, then sure it takes space:

int **addr2 = &addr; 

You can think of the memory as a series of boxes. Assuming 4-byte int and pointers, and little-endian byte order, your scenario might look like:

+----------+--+--+--+--+ | Address  |Data bytes | +----------+--+--+--+--+ |0x00000000|64|00|00|00| +----------+--+--+--+--+ |0x00000004|00|00|00|00| +----------+--+--+--+--+ 

The address is shown on the left, the bytes contained at that location on the right. You can see the value 100 stored in the first four bytes (100 decimal is 0x64 in hex). The second 4-byte location holds the value 0x00000000, which is the address of x. The address 0x00000004 is not stored anywhere.

Now if we add a second pointer, we'd use more memory:

+----------+--+--+--+--+ |0x00000008|04|00|00|00| +----------+--+--+--+--+ 

That would be the memory used to represent the addr2 pointer, and you can see that it contains the address of addr, i.e. 0x00000004.

Dereferencing addr2 with the expression *addr2 would yield the value at address 0x00000004, i.e. 0x00000000 with the type int *. De-referencing that once more yields the int at that address, i.e. 0x00000064.

Since this memory layout is chosen by the compiler, it "knows" the addresses involved, and can just substitute so that when code references addr2, it generates instructions that manipulate address 0x00000008, and so on. In real code this would probably all happen on the stack, but the principle is the same.

Final note: do heed @Phil Perry's advice; the above simplifies and makes ocncrete a lot of things that are meant to be somewhat abstract. This is how it really does work on many current-day architectures, but many of the things mentioned are not guaranteed by C so you cannot really depend on them to always hold true. I meant the above as an illustration to (hopefully) make the concepts slighly less vague.

like image 129
unwind Avatar answered Sep 22 '22 14:09

unwind


If you store an address, naturally, you need some place to store it (i.e. another address), so you can continue this for as long as you wish. This ends precisely when you want it to end.

int         a = 1; int       *pa = &a; int     **ppa = &pa; int   ***pppa = &ppa; int ****ppppa = &pppa; ... 

Here is a quick analogy: let's say you have a notebook with numbered pages and lines. Suppose you make a note on page 3, line 8, and then you want to reference that note from some other place. Perhaps you can write on page 7, line 20, a reference "see note on page 3, line 8". Now the reference has its own "address" - namely, page 7, line 20. You can reference it, too - on page 8, line 1, you could write "see note on page 7, line 20". You can continue this chain of referencing for as long as you wish.

like image 37
Sergey Kalinichenko Avatar answered Sep 22 '22 14:09

Sergey Kalinichenko