Naren
Naren

Reputation: 159

Difference between sizeof(*p) and sizeof(p)?

I am using code below and getting different values.

int *p;

printf("Size of *p = %d", sizeof(*p));   // Here value is 4

printf("Size of p = %d", sizeof(p));    // Here value is 8

Can any one please explain, what exactly is the reason behind this?

Upvotes: 3

Views: 11158

Answers (4)

SJP
SJP

Reputation: 31

Here is the syntax of a pointer variable:

var_type *variable;

e.g. int *k;

  • '*' indicates that the variable 'k' is a pointer.

A pointer is also a variable whose value equals the address of the variable it points to.

Let's say we have a variable 'v' of type 'integer' that has a value of '100':

int v = 100;

When v is declared, it is stored in an address (a location on the memory). The space it occupies depends on the type of the variable. Let's assume a 64-bit system for the illustration below (variable type - size):

char - 1 byte; int - 4 bytes; float - 4 bytes

Thus, for an integer (int), the memory allocated is 4 bytes - at an address.

Recall that, int v = 100;

The address of v can be gotten with the ampersand operator prefix & (in C language): &v

printf("The address of v is: %lu", &v); //this prints the address of v in memory

Now, let's make use of our pointer - k:

k = &v; //pointer is assigned a value that is equal to the address of variable v

Take note that

The default size of any "pointer" (assuming a 64-bit system) is 8 bytes.

This means that our pointer k has a size of 8 bytes.

printf("The size of pointer k is: %d", sizeof(k));

printf("Size of p = %d", sizeof(p)); // Here value is 8

Then the size of *k (which equals the value of the address of "integer" v) is 4 bytes.

Recall that the sizeof(int) = 4 bytes.

printf("The size of *k is: %d", sizeof(*k));

printf("Size of *p = %d", sizeof(*p)); // Here value is 4

I really hope this clarifies.

Upvotes: 0

Umair
Umair

Reputation: 346

sizeof(p) is the size of the pointer itself. It depends on the size of the address bus. Which means for a 64-bit system, the address bus size will be 64-bit (8 bytes) so pointer will be 8 bytes long (that shows your system is 64-bit). And on a 32-bit system, it's size will be 32-bit(4 bytes).

sizeof(*p) is the size of pointer type i.e. int here. So usually int is of 32-bit long that is 4 bytes.

Upvotes: 3

unalignedmemoryaccess
unalignedmemoryaccess

Reputation: 7441

sizeof(*p) returns size of type what the pointer points to while sizeof(p) returns size of pointer itself.

In your case *p = int and sizeof(int) = 4 on your machine, while you need 8 bytes to store memory address (address where p points to).

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409166

For any pointer variable p, the variable p itself is the pointer and the size of it is the size of the pointer. *p is what p is pointing to, and the size of *p is the size of what is being pointed to.

So when sizeof(p) reports 8 then you know that a pointer on your system is 8 bytes, and that you're probably on a 64-bit system.

If sizeof(*p) reports 4 then you know that the size of int (which is what p is pointing to in your case) is 4 bytes, which is normal on both 32 and 64 bit systems.

You would get the same result by doing sizeof(int*) and sizeof(int).

Oh and a last note: To print the result of sizeof (which is of type size_t) then you should really use the "z" prefix, and an unsigned type specifier (since size_t is unsigned). For example "%zu". Not doing that is technically undefined behavior.

Upvotes: 8

Related Questions