nano
nano

Reputation: 17

What addresses do the pointer store?

I am currently in the learning process of pointers in C. I came to know that, a pointer is a variable which stores the address of another variable. So when I did something like,

#include <stdio.h>

int main()
{
    int x = 10;
    int *ptr;
    ptr = &x;
    printf("%d" ,ptr); 
   

The above gave me the address in integer values.
My question is, the pointer variable ptr stores address of variable of type int. As per my PC, int is taking 4 bytes which is 32 bits. As per my understanding each bit has a separate memory address.

So what is the address pointer will point to? Will it point to the first bits memory address or something else? Please let me know.

Please correct me if my understanding is wrong.

Upvotes: 0

Views: 777

Answers (4)

John Bode
John Bode

Reputation: 123548

On systems like x86, each individual byte has its own address. For multi-byte objects like ints or doubles, the address of the object is the address of its first byte. On a little-endian system like x86, the first byte is the least-significant byte, while on a big-endian system like Power the first byte is the most significant byte:

int x = 0x01234567;

 A      A+1    A+2    A+3      big-endian
+––––––+––––––+––––––+––––––+
| 0x01 | 0x23 | 0x45 | 0x67 |
+––––––+––––––+––––––+––––––+
 A+3    A+2    A+1    A        little-endian

Most architectures have alignment restrictions such that multi-byte entities must have an address that is a multiple of 2 or 4. This is why struct types may have "padding" bytes between members.

Addressing and byte ordering are a function of the underlying architecture, not the C language. There are some word-addressed systems where each individual byte does not have its own address, so pointers to smaller types like char may need to include an offset into the word. Representation of pointer types can vary.

Unless you’re working on bare metal, the address values you’re working with are virtual addresses, not physical.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

The minimum addressable unit is byte. Independent on how much bytes an object of the type int occupies a pointer to such an object points to the first byte of the extent of memory occupied by the object.

From the C Standard

3.5 1 bit

unit of data storage in the execution environment large enough to hold an object that may have one of two values

2 NOTE It need not be possible to express the address of each individual bit of an object.

3.6 1 byte addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

Pay attention to that this call

printf("%d" ,ptr); 

invokes undefined behavior.

If you want to output the value of a pointer you should use the conversion specifier p. For example

printf("%p\n" , ( void * )ptr); 

Upvotes: 1

Rob
Rob

Reputation: 15158

A memory address is the location of one byte. A 32-bit location is the location on a four byte boundary.

So memory address 0x0000 is equal to the first 32-bit memory location. Address 0x0004 would be equal to the next four-byte boundary or, in other words, the next 32-bit location.

Then that just leaves the issue of big-endian and little-endian.

Upvotes: 2

klutt
klutt

Reputation: 31409

As per my understanding each bit has a separate memory address.

No, every byte has a memory address. You'll have to use an additional offset to get individual bits. You cannot make a pointer point at a single bit.

So what is the address pointer will point to? Will it point to the first bits memory address or something else? Please let me know.

Nope. It points to the object as a whole. You cannot say which byte in the object, because that depends on endianness.

Furthermore, when compiled with -Wall -Wextra this gives a warning.

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’

You're using the wrong format specifier for printing a pointer. %d is for int. Your pointer, however, has the type int* which is not int. If you want to print the address, use this instead:

printf("%p", (void*)ptr); 

Upvotes: 1

Related Questions