quantum231
quantum231

Reputation: 2585

What is the explaination for this simple pointer program output

I tried to understand the size of address used to store variables and pointers, pointers-pointers and pointers-pointers-pointers. The results are kind of confusing.

Here is the code:

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

int main(void)
{
    char    *** ppptr_string = NULL;
    int     *** ppptr_int    = NULL;
    double  *** ppptr_dbl    = NULL;

    char c=0; int i=0; double  d=0;

    printf("\n %d   %d   %d   %d   %d\n", sizeof(&ppptr_string),   
    sizeof(ppptr_string),  sizeof(*ppptr_string), sizeof(**ppptr_string), 
    sizeof(***ppptr_string));
    printf("\n %d   %d   %d   %d   %d\n", sizeof(&ppptr_int),   sizeof(ppptr_int),    
    sizeof(*ppptr_int),   sizeof(**ppptr_int),   sizeof(***ppptr_int));
    printf("\n %d   %d   %d   %d   %d\n", sizeof(&ppptr_dbl),   sizeof(ppptr_dbl),    
    sizeof(*ppptr_dbl),   sizeof(**ppptr_dbl),   sizeof(***ppptr_dbl));


    printf("\n  sizeof(char) = %d,   sizeof(int) = %d,   sizeof(double) = %d", 
    sizeof(c), sizeof(i), sizeof(d));
    printf("\n sizeof(&char) = %d,  sizeof(&int) = %d,  sizeof(&double) = %d", 
    sizeof(&c), sizeof(&i), sizeof(&d));

getch();
return 0;
}

Now the confusion. I can see that a variable address is always 2 bytes long on this machine. Regardless of type of the variable and regardless of the whether its a pointer variable. But why do I get size of 4 for so many entries in here? The pointer has size 4 always regardless of the type. The >address< at which the variable is stored is of size 2. And the content pointed to has a sized depending on the type.

Why do I get 4s in the output for sizeof??

My output from Borland C++ 5.02 enter image description here

Upvotes: 0

Views: 295

Answers (2)

craig65535
craig65535

Reputation: 3572

I think what's happening is that you're getting near (16-bit) pointers for local variables, but a pointer declared as type * is a far (32-bit) pointer.

It's a quirk of working on a 16-bit Intel processor (or a 32-bit processor in "real mode"), e.g. in DOS, where you only have access to 1 MB of memory (or 640 kB in practice). The upper 16 bits of a far pointer are a segment (a 64k page in memory), and the lower 16 bits are an offset.

http://en.wikipedia.org/wiki/Real_mode

http://wiki.answers.com/Q/What_are_near_far_and_huge_pointers_in_C

Answerers not able to reproduce this are most likely using a 32-bit (or more) OS on a 32-bit (or more) processor.

Upvotes: 0

Zeta
Zeta

Reputation: 105876

If you have a type T and a pointer on pointer like T*** ptr, then ptr, *ptr, **ptr are pointers themselves. You're probably working on a 32bit system (or compiling a 32bit application), so sizeof(ptr) == sizeof(*ptr) == sizeof(**ptr):

--- Program output ---

 4   4   4   4   1

 4   4   4   4   4

 4   4   4   4   8

  sizeof(char) = 1,   sizeof(int) = 4,   sizeof(double) = 8
 sizeof(&char) = 4,  sizeof(&int) = 4,  sizeof(&double) = 4

&ptr is an address/a pointer on T***, so its size is 4 too. Only if you dereference the pointer to its maximum level (***ptr) you will have the actual type and not another pointer.

Upvotes: 3

Related Questions