Home > Net >  Struct definition Function of type Static
Struct definition Function of type Static

Time:03-30

I came across the below line in one of the code:

static DATA_BLOCK_SOX_s data_blockSox = {.header.uniqueId = DATA_BLOCK_ID_SOX};

I want to know what form of Syntax is this and what is exactly happening here Thanks

CodePudding user response:

That's a C struct initializer with named fields (designated initializers).

static DATA_BLOCK_SOX_s data_blockSox = {.header.uniqueId = DATA_BLOCK_ID_SOX};

Creates a DATA_BLOCK_SOX_s in load-time allocated storage called data_blockSox scoped to this file, initializing all fields to binary zero except for header.uniqueId (where header is clearly a nested struct or union) which is initialized to DATA_BLOCK_ID_SOX.

I have used indentation to infer file scope. That could be incorrect. If it's inside a function it should be indented, but as we know, the compiler doesn't care.

In the unlikely case that you actually care about systems for which NULL is not binary zero, it has been pointed out that pointer fields should be initialized to NULL even on systems that have NULL as not binary zero. I have only ever seen that spec not followed.

CodePudding user response:

This looks like C99. And should be something like:

struct Point {int x, y;}; // This would be the stand-in for DATA_BLOCK_SOX_s
typedef struct Point Point;

static Point point = { .y = 10, .x = 5 }; // here the members y and x get their values

In your case there is a nested struct called header. This as a member calles uniqueId.

  •  Tags:  
  • c
  • Related