Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How pointers in C/C++ actually store addresses?

If an int is stored in memory in 4 bytes each if which having a unique address, which address of these four addresses does a pointer to that int store?

like image 893
Mohamed Abdikadir Avatar asked Jul 18 '20 12:07

Mohamed Abdikadir


People also ask

How does pointer store address?

A pointer to int usually stores the address value of the first byte (which is stored at the lowest memory address) of the int object.

How are addresses stored in C?

When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.

Do pointers have addresses in C?

To use pointers in C, we must understand below two operators. To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.

Do pointers hold addresses?

The main feature of a pointer is its two-part nature. The pointer itself holds an address. The pointer also points to a value of a specific type - the value at the address the point holds. The pointer itself, in this case, is p.


1 Answers

A pointer to int (a int*) stores the address of the first byte of the integer. The size of int is known to the compiler, so it just needs to know where it starts. How the bytes of the int are interpreted depends on the endianness of your machine, but that doesn't change the fact that the pointer just stores the starting address (the endianness is also known to the compiler).

like image 72
Jesper Juhl Avatar answered Sep 27 '22 18:09

Jesper Juhl