user2251001
user2251001

Reputation: 51

Subtle differences in C pointer addresses

What is the difference between:

*((uint32_t*)(p) + 4);
*(uint32_t*)(p+4);

or is there even a difference in the value?

My intuition is that in the later example the value starts at the 4th index of the array that p is pointing at and takes the first 4 bytes starting from index 4. While in the first example it takes one byte every 4 indices. Is this intuition correct?

Upvotes: 0

Views: 71

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The p+4 expression computes the address by adding 4*sizeof(*p) bytes to the value of p. If the size of *p is the same as that of uint32_t, there is no difference between the results of these two expressions.

Given that

p is an int pointer

and assuming that int on your system is 32-bit, your two expressions produce the same result.

Upvotes: 2

Related Questions