C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 9)
9.
What will be the output of the program if the size of pointer is 4-bytes?
#include<stdio.h>

int main()
{
    printf("%d, %d\n", sizeof(NULL), sizeof(""));
    return 0;
}
2, 1
2, 2
4, 1
4, 2
Answer: Option
Explanation:

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

Discussion:
27 comments Page 3 of 3.

Rahul said:   1 decade ago
Thanks krishan.

Krishan said:   1 decade ago
NULL is always a pointer which points to nothing.
that's why it will always be of the size of any other pointer i.e. 4 bytes.
but what's interesting is "" is a string constant and sizeof(const string) always returns the string length.
though you can still write

char* szptr = "";
printf("%d",sizeof(szptr));

which will give an output 4. but if you write like this,

char* szptr[] = "";
printf("%d",sizeof(szptr));

which will output 1.

Purushotham said:   1 decade ago
Here null is act as a pointer? can expalin any one please.

Jack said:   1 decade ago
Hi Bijan
please find the below
printf("%d",sizeof("jack"));--->5
because j-1
a-2
c-3
k-4
\o-5
similarly printf("%d",sizeof(""));--->1
because "\0" will be included at the end of the string

Bijan said:   1 decade ago
If you are not clear with output then sizeof("") is equivalent to sizeof(char) where char value = 0, so it would be 1. However, sizeof(NULL) is implementation defined. So if size(int) then 4.

Sunil kumar said:   1 decade ago
Can anyone explain this program ?

Murthy said:   1 decade ago
What is null ?


Post your comments here:

Your comments will be displayed after verification.