Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
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.

>Solution :

Your compound ;literal defines a null pointer instead of an object of the structure type

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

Instead write

move_stats *mv_s = &(move_stats){0};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading