gpt4 book ai didi

c - 初始化结构

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:21 26 4
gpt4 key购买 nike

海湾合作委员会 4.5.1 c89

使用以下标志编译:-Wall、-Wextra

我尝试使用默认值初始化以下结构。但是,我收到以下警告:

static struct Device {
char *name;
char *config;
} *app = { NULL, NULL };

警告:

warning: initialization from incompatible pointer type
warning: excess elements in scalar initializer

但是,如果我通过声明一个非指针来执行以下操作,我不会遇到任何问题,即

 static struct Device {
char *name;
char *config;
} app = { NULL, NULL };

这是为什么?

非常感谢任何建议,

最佳答案

您不能以这种方式初始化指针。你可以这样做:

static struct Device {
char *name;
char *config;
} *app = NULL;

初始化指向NULL 的指针。指针只是一个内存地址。您不能将 { NULL, NULL } 分配给内存地址。

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

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