Casting NULL pointers

Viewed 2980

Please consider the following program. Given four different pointers, it prints them as unsigned long.

#include<stdio.h>

typedef unsigned long ulong;

int main(){
    char        *pc = NULL;
    int         *pi = NULL;
    double      *pd = NULL;
    long double    *pld = NULL;
    printf("%5lu%5lu\n%5lu%5lu\n%5lu%5lu\n%5lu%5lu\n",
        (ulong)(pc + 1),(ulong)(pi + 1),
        (ulong)(pd + 1),(ulong)(pld + 1),  
        (ulong)(pc + 1),(ulong)(pld + 1)
        (ulong)(pc), (ulong)(pc));
    return 0;
}

Well, I am not totally sure that I gave the results the correct meaning:

1    4
8   16
1   16
0    0

With pc + 1 and pi + 1 we have 1 and 4 respectively. My system, most likely, assign 1 byte for a pointer to char and 4 for a pointer to int. On this base, I deduced that the program gives me the amount of memory assigned made available to these pointers.

But pc and pi return both zero. Why? If my deduction was correct, should not 1 and 4 bytes of memory be assigned in any case to pc and pi?

3 Answers
Related