Possible Duplicate:
Is NULL always zero in C?
The C standard states the following for calloc()
:
The calloc function allocates space for an array of nmemb objects, each of whose size is size. The space is initialized to all bits zero.
with the following caveat relating to all bits zero:
Note that this need not be the same as the representation of floating-point zero or a null pointer constant.
Test program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
char** list = calloc(10, sizeof(*list));
int i;
for (i = 0; i < 10; i++)
{
printf("%p is-null=%d\n", list[i], NULL == list[i]);
}
free(list);
return 0;
}
I built and executed this program with the following compilers:
- VC7, VC8, VC9, VC10
- gcc v4.1.2, gcc v4.3.4
- Forte 5.8, Forte 5.10
In all cases all bits zero is a NULL
pointer (unless I made a mistake in the test program).
What is the reason a NULL
pointer is not guaranteed by the C standard to be all bits zero ? Out of curiousity, are there any compilers where all bits zero is not a NULL
pointer ?