1

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
3
  • 1
    Whay compiler, what linker, what processor? 32 or 64-bit? What is an int - how big is it? Does it really matter much? I mean, how did you even notice? Commented Apr 28, 2012 at 21:31
  • 1
    It's interesting that changing the first int to long works as you expect. Commented Apr 28, 2012 at 21:51
  • Ya its working good for long... :) Commented Apr 28, 2012 at 21:55

1 Answer 1

4

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.

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

5 Comments

Yes %p is converting the int address into hexa..hope I am correct.. but still whats the reason for stack taking address like that...
@Vutukuri The stack layout isn't fixed in stone, nor is it standard. Each compiler has absolute freedom in choosing how to arrange local variables. Passing arguments is another story (cdecl, stdcall).
Okay so u mean to say if I execute the same program on a diff compiler I might get diff results?
@Vutukuri Yup. Also, you might get different results by simply changing the switches - for example optimization flags.
@Vutukuri: if you use %p, then you must cast your pointer to void*. If you want to print an integral value, you can also cast to uintptr_t and use the appropriate macro from stdint.h.

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.