问题描述
我知道要使用的释放calloc申请的内存,对所有位写0,然后返回一个指针。
I know that calloc request memory to be used, writes 0 on all the bits and then returns a pointer to it.
我的问题是:如果我使用释放calloc与包含指针的结构,将这些指针有NULL值,或者我必须将它们设置为指向NULL
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;
所以基本上,我用释放calloc与结构后,将其命名为和P点为NULL?
So basically, will name and p point to NULL after I've used calloc with struct a?
谢谢!
推荐答案
不知怎的,你已经得到了很多不正确的答案。 C不要求空指针重新presentation是所有零位。很多人误以为这样做,因为一个整型常量前pression值为0,转换为指针,成为一个空指针。
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.
随着中说,在所有现实世界的系统中,空指针都是零位,而释放calloc
是一个完全合理的方式来获得一个空指针 - 初始化指针阵列中的现实世界
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.
这篇关于释放calloc,结构与C指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!