0

Refer the following C program and while incrementing the pointer (i.e. p), it is correctly incrementing by 4 bytes. While if I try to increment the pointer to pointer (i.e. pp), then same is incrementing by 8 bytes. And I am not understanding why is it happening in this way and may be i have misunderstanding in the concept.

#include <stdio.h>

int main()
{
float a = 5, *p, **pp;
p = &a;
pp = &p;
printf("a=%f, p=%p, pp=%p\n", a, p, pp);
a = a + 1;
p = p + 1;
pp = pp + 1;
printf("a=%f, p=%p, pp=%p\n", a, p, pp);

return 0;
}

output:
a=5.000000, p=0x7ffc93c93374, pp=0x7ffc93c93368
a=6.000000, p=0x7ffc93c93378, pp=0x7ffc93c93370

0

2 Answers 2

4

Pointer arithmetic is done in units of the size of the type that the pointer points to. On your system, sizeof(float) is 4, so incrementing p adds 4 bytes to it. But sizeof(float*) is 8 because it's a 64-bit system, so incrementing pp adds 8 bytes to it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Barmer for your quick reply. I am using 64bit OS and but what is the main reason for different results. Even I have tried <sizeof(float **)> which is also providing result of 8 bytes.
That's the reason. sizeof(float *) == 8, so adding 1 to float ** means adding 8 bytes.
sizeof(float **) is not relevant to this, that would only matter if you did float ***ppp = &pp;
@Barmer: Now I have realized from you replies and because of 64bit OS i am little confused. Incrementing any level of pointers then it is going to increment only 8 bytes. Because most of the times i had been used 32 bit OS and suddenly migrated to 64 bit OS and this makes me confused.
1

To append the answer of @Barmar I would like to point out that if you have an array

T a[N];

where T is some type and N is some value then after such a declaration of a pointer like

T *p = a;

The pointer p will point to the first element of the array a. This declaration is equivalent to

T *p = &a[0];

If to increment the pointer p it is naturally to assume that it will point to the second element of the array a that is its value will be the value of the expression &a[1]. S0 you need to add to the original value of the pointer p the value that is equal to the value of the size of an element of the array a that is the value equal to sizeof( T ).

Such a calculation is named the pointer arithmetic.

Thus the expression

p + 1

or

++p

means to add the value sizeof( T ) to the value stored in the pointer p. As a result the pointer expression will point to the next element of the array.

4 Comments

I am well understood the pointer arithmetic and your explanation is also known to me. But my query is for incrementing pointer to pointer (i.e. pp variable in my code).
@BhaskaraRaoSanta Make one more step in understanding and substitute in my answer the abstract type T for float *. So incrementing the pointer means adding to its value the value sizeof( float * )
Thanks your reply and now i have realized and till now i am looking in the point of incrementing float base type of pointer. And it is varying if we go for one more levels in incrementing pointers such pointer to pointer etc.
@BhaskaraRaoSanta The size of a pointer depends on the used system and usually equal to 4 (in 32-bit system) or 8 (in 64-bit system). It does not depend on the size of the type an object of which is pointed to by the pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.