gpt4 book ai didi

c - 使用宏初始化结构

转载 作者:太空狗 更新时间:2023-10-29 14:54:58 25 4
gpt4 key购买 nike

我一直在寻找,但找不到任何东西。考虑这个结构

typedef struct student
{
char name[40];
char grade;
}Student;

如何制作一个宏来初始化带有参数的结构?类似的东西

Student John = STUDENT(John, A); 

其中 STUDENT 是定义的宏

最佳答案

#define STUDENT(name, grade) { #name, grade }

然后 Student John = STUDENT(John, 'A'); 将被展开为

Student John = { "John", 'A' };

关于c - 使用宏初始化结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com