Teja
Teja

Reputation: 13534

Variation in Pointer address values

I tested a small program which is written below.My question is why there is a 12 bytes difference between the pointer to a value and a pointer to the first pointer.But if you look at other pointer addresses there is only a difference of 8 bytes every time.I executed this program multiple times and always I see this difference.Can anyone explain me what could be the reason?Thanks in advance..

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
        int val;
        int *ptr;
        int **ptrptr;
        int ***ptrptrptr;
        int ****ptrptrptrptr;
        int *****ptrptrptrptrptr;

        val=10;
        ptr=&val;
        ptrptr=&ptr;
        ptrptrptr=&ptrptr;
        ptrptrptrptr=&ptrptrptr;
        ptrptrptrptrptr=&ptrptrptrptr;

        printf("Value-%d\n",val);
        printf("Value address - %d\n",ptr);
        printf("Pointer address - %d\n",ptrptr);
        printf("Pointer Pointer Address -%d\n",ptrptrptr);
        printf("Pointer Pointer Pointer Address -%d\n",ptrptrptrptr);
        printf("Pointer Pointer Pointer Pointer Address -%d\n",ptrptrptrptrptr);

        return 0;
}

The results are:

Value-10
Value address - -1308521884
Pointer address - -1308521896
Pointer Pointer Address --1308521904
Pointer Pointer Pointer Address --1308521912
Pointer Pointer Pointer Pointer Address --1308521920

Upvotes: 1

Views: 146

Answers (1)

cnicutar
cnicutar

Reputation: 182649

It's just the stack layout your compiler chose, f.e. it could be for alignment reasons. Things would most likely still work with other layouts.

Side note, you should use %p to print addresses.

Upvotes: 4

Related Questions