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?
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With