cpointersnullstructurecalloc

Calloc with structure with pointers in C


I know that calloc request memory to be used, writes 0 on all the bits and then returns a pointer to it.

My question is: if I use calloc with a structure that contains pointers, will those pointers have the NULL value or do I have to set them to point to NULL?

struct a{
char * name;
void * p;
}* A;

So basically, will name and p point to NULL after I've used calloc with struct a?

Thanks!


Solution

  • Somehow you've gotten a lot of incorrect answers. C does not require the representation of null pointers to be all-zero-bits. Many people mistakenly think it does, since an integer constant expression with value 0, converted to a pointer, becomes a null pointer.

    With that said, on all real-world systems, null pointers are all-zero-bits, and calloc is a perfectly reasonable way to get a null-pointer-initialized pointer array in the real world.