简体   繁体   English

这个C结构分配是什么意思?

[英]What does this C struct assignment mean?

I'm trying to read through some C source code and I've come across a structure assignment that I don't understand: 我正在尝试阅读一些C源代码,并且遇到了我不理解的结构分配:

static struct sym_table *sym_array = (struct sym_table *) 0;

I understand that the left-hand-side is trying to initialize a pointer to a structure of type 'sym_table' (which is defined elsewhere); 我知道左侧正在尝试初始化一个指向“ sym_table”类型的结构的指针(该符号在其他地方定义); however, I am struggling to understand the right-hand-side. 但是,我正在努力理解右侧。 It's probably fairly simple, but I can't find any similar examples of this sort of assignment in any of the online C tutorials that I've seen. 这可能很简单,但是我在所见过的任何在线C教程中都找不到这种分配的类似示例。

If anyone can shed some light on it, I would really appreciate it! 如果有人能对此有所了解,我将不胜感激!

static struct sym_table *sym_array = (struct sym_table *) 0;

is a slightly cumbersome way to set a pointer to NULL , NULL being defined in stdio.h as ((void *)0) . 将指针设置为NULL方法有点麻烦,在stdio.h中将NULL定义为((void *)0) You'd be better off with: 您最好拥有:

static struct sym_table *sym_array = NULL;

The right-hand-side is the equivalent of NULL pointer. 右侧等效于NULL指针。 But here , it is a NULL pointer of type (struct sym_table *) 但是在这里,它是类型为(struct sym_table *)的NULL指针

this the Stroustrup way. 这就是Stroustrup的方式。 He hates macros. 他讨厌宏。 So he always uses 0 in his code or eventually when keyword nullptr was introduced the nullptr . 因此,他总是在代码中使用0 ,或者最终在关键字nullptr被引入nullptr I think it is from the C++ code or from the program written by someone who usually uses C++. 我认为它来自C ++代码或通常使用C ++的人编写的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM