代码之家  ›  专栏  ›  技术社区  ›  IAbstract

初始化结构时如何增加值?

c
  •  2
  • IAbstract  · 技术社区  · 1 年前

    使用初始化结构 unsigned short id 使用 #define ...

    
    static unsigned short mId = 0;
    
    typedef struct
    {
        /* data */
        unsigned short id;
        const char *name;
        bool enabled;
    } Machine;
    
    #define CREATE_MACHINE(_mName_) Machine m##_mName_ = {mId++, #_mName_, false};
    
    

    如何递增 mId 用于下一个实例初始化? mId++ 在初始值设定项中不允许。

    因此,我还尝试了一个功能,正如一条评论所建议的那样:

    static Machine _create_machine(const char *name)
    {
        unsigned short id = mId++;
        Machine m = {id, name, false};
    
        return m;
    }
    
    // corrected typo in method parameter
    #define CREATE_MACHINE(_mName_) Machine m##_mName_ = _create_machine(#_mName_);
    

    不能在常量表达式中使用函数。

    每个请求的完整源。。。

    // main.c
    #include <stdio.h>
    #include <stdbool.h>
    
    #include "../include/machine.h"
    
    MAKE_MACHINE(test)
    
    int main()
    {
        PLACE(mtest);
        printf("placed machine[%d] enabled: %s\n", mtest.id, B(mtest.enabled);
    
        DROP(mtest);
        printf("dropped machine[%d] enabled: %s\n", mtest.id, B(mtest.enabled);
    }
    
    // machine.h
    #ifndef MACHINE_H
    #define MACHINE_H
    
    #include "op/data_types.h"
    
    static USHORT mId = 0;
    
    enum MachineState
    {
        M_IDLE,
        M_RUN,
        M_LOCK
    };
    
    typedef struct
    {
        /* data */
        const USHORT id;
        const CHAR *name;
        BOOL enabled;
    } Machine;
    
    static Machine _make_machine(const char *name)
    {
        USHORT id = mId++;
        Machine m = {id, name, false};
    
        return m;
    }
    
    static void _place_machine(Machine *machine)
    {
        machine->enabled = true;
    }
    static void _drop_machine(Machine *machine)
    {
        machine->enabled = false;
    }
    
    #define MAKE_MACHINE(_mName_) Machine m##_mName_ = _make_machine(#_mName_);
    
    #define PLACE(_machine_) _place_machine(&_machine_);
    #define DROP(_machine_) _drop_machine(&_machine_);
    
    #endif //  MACHINE_H
    
    1 回复  |  直到 1 年前
        1
  •  2
  •   Stephen Newell    1 年前

    一个函数会起作用。完整的工作示例:

    #include <stdio.h>
    #include <stdbool.h>
    
    static unsigned short mId = 0;
    
    typedef struct
    {
        /* data */
        unsigned short id;
        const char *name;
        bool enabled;
    } Machine;
    
    static Machine make_machine(const char * name) {
        // lock mutexes, atomic operations, etc.
        unsigned short next_id = mId++;
        // unlock mutexes if necessary
        Machine ret = {next_id, name, false};
        return ret;
    }
    
    #define CREATE_MACHINE(_mName_) Machine m##_mName_ = make_machine(#_mName_)
    
    int main() {
        CREATE_MACHINE(hello);
        CREATE_MACHINE(world);
        printf("%u: %s\n", mhello.id, mhello.name);
        printf("%u: %s\n", mworld.id, mworld.name);
        return 0;
    }
    

    因为函数没有以同样的方式受到约束,我们可以在那里做额外的工作,比如增量 mId 。如果要使用不同的递增ID变量, make_machine 可能需要一个 unsigned short* 它在函数本身中进行操作,并且仍然可能被宏隐藏。