Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name unnamed fields in a C structure?

Let's consider this structure:

struct {
    int a;
    int _reserved_000_[2];
    int b;
    int _reserved_001_[12];
    int c;    
};

The reserved fields should never be read or written. My structure represents a descriptor to address an FPGA where I got a lot of reserved fields. I eventually named them with random named because after years the initial ascending numbering doesn't mean anything anymore.

So I have now:

struct {
    int a;
    int _reserved_3hjdds1_[2];
    int b;
    int _reserved_40iuljk_[12];
    int c;    
};

It would be more convenient to have just empty fields instead:

struct {
    int a;
    int;
    int b;
    int;
    int c;    
};

But it doesn't work.

What other alternative can I just that avoid finding unique name for reserved fields?

like image 851
nowox Avatar asked Mar 16 '17 15:03

nowox


1 Answers

It should be possible to achieve what you want with a bit of macro-magic:

#include <stdint.h>

#define CONCAT(x, y) x ## y
#define EXPAND(x, y) CONCAT(x, y)
#define RESERVED EXPAND(reserved, __LINE__)

struct
{
  uint32_t x;
  uint32_t RESERVED;
  uint16_t y;
  uint64_t RESERVED[10];
} s;

This gives you identifiers such as reserved11, reserved13, but the names obviously don't matter.

like image 83
Lundin Avatar answered Sep 27 '22 22:09

Lundin