简体   繁体   English

C 堆栈上的空结构指针

[英]C Empty Struct Pointer on Stack

I would like to initialize a struct with all fields as zero and immediately assign it to a pointer.我想初始化一个所有字段为零的结构并立即将其分配给一个指针。 The struct will only need to be used within static functions whose lifetime is completely contained within the calling function.该结构只需要在其生命周期完全包含在调用函数中的静态函数中使用。

This is the line I currently have这是我目前拥有的线路

move_stats *mv_s = (move_stats *){0};

The struct is defined in a header file as该结构在头文件中定义为

typedef struct {
  byte open_threes;
  byte open_fours;
  byte half_open_fours;
  bool overline;
  bool five;
} move_stats;

byte is an alias for unsigned char. byte是 unsigned char 的别名。 Will this create the struct in the way I want it to as written?这会按照我想要的方式创建结构吗? I could not find a question exactly like this, apologies if it is a duplicate.我找不到完全这样的问题,如果它是重复的,请道歉。

Your compound ;literal defines a null pointer instead of an object of the structure type您的复合 ;literal 定义了一个空指针而不是结构类型的对象

move_stats *mv_s = (move_stats *){0};

Instead write而是写

move_stats *mv_s = &(move_stats){0};

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

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