带有 C 指针结构的 Calloc [英] Calloc with structure with pointers in C
问题描述
我知道要使用的 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 与结构 a 一起使用后,name 和 p 会指向 NULL 吗?
So basically, will name and p point to NULL after I've used calloc with struct a?
谢谢!
推荐答案
不知何故,你得到了很多不正确的答案.C 不要求空指针的表示为全零位.很多人错误地认为是的,因为一个值为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.
这篇关于带有 C 指针结构的 Calloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!