c - 在运行时修改结构体字段

标签 c c-preprocessor

我不确定这是否可行,但我需要在运行时修改某些结构的字段。因此,根据运行可执行文件时传递的参数,它应该生成不同类型的结构。

#if (PARAM_SET)
    typedef struct{
        char fetch[FILE];
        uint32_t bin;
        uint32_t bin_1;
        uint32_t bin_2;
    }
    HELPER;

    typedef struct __attribute__ ((packed)){
         char job[4];
         uint32_t last;
         uint32_t liv_1;
         uint32_t liv_2;
         uint32_t dipr;
         uint32_t notch;
         uint32_t cnt;
         uint32_t max;
         char path[MAX_HEAD_PATH];
         HELPER for[MAX_NUM];
    }
    BOARD;

#else
    typedef struct __attribute__ ((packed)){
        char job[4]
        uint32_t nxt;
        uint32_t prv;
        uin32 boards;
        char path[MAX_NUM];
    }
    BOARD;
#endif

最佳答案

您无法在运行时(即程序编译后)更改结构。

但是您可以提供两种类型的结构(具有不同的名称)并调整您的代码,以便它根据运行时评估的参数/条件使用正确的类型。

关于c - 在运行时修改结构体字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43926823/

相关文章:

更改 argp 上的默认输出流

c - 反转列表会导致段错误

C 翻译阶段 4

带内存的 C++ 宏

c++ - 是否有将 -U__STRICT_ANSI__ 添加到 XCode build设置的正确位置?

c++ - 位数 : preprocessor magic vs modern C++

c - 在 C 中的 block 范围内的变量声明之后使用 goto

c++ - 计算位和位反相中有多少个

c - C 内存中的未知错误

c - 使用 C 预处理器读取变量