gpt4 book ai didi

使用结构时发出警告

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

我在很长一段时间后重新访问 C,并对编译器生成的警告感到困惑。这是相关代码:

struct Unit 
{
char str1[100];
char str2[100];
short expected;
};

int main()
{
struct Unit arr[] =
{
{NULL, NULL, 0},
{NULL, "string", -1},
{"string", NULL, 1},
{"string", "string", 0},
{"string1", "string2", -1},
{"string2", "string1", 1},
{"str", "string", -1},
{"string", "str", 1}
};

printf("%d\n", arr[0].expected);
return 0;
}

编译后,我得到:

my_strcmp.c: In function ‘main’:
my_strcmp.c:64:9: warning: initialization makes integer from pointer without a cast [enabled by default]
{NULL, NULL, 0},
^
my_strcmp.c:64:9: warning: (near initialization for ‘arr[0].str1[0]’) [enabled by default]
my_strcmp.c:64:9: warning: initialization makes integer from pointer without a cast [enabled by default]
my_strcmp.c:64:9: warning: (near initialization for ‘arr[0].str1[1]’) [enabled by default]
my_strcmp.c:65:9: warning: initialization makes integer from pointer without a cast [enabled by default]
{NULL, "string", -1},
^
my_strcmp.c:65:9: warning: (near initialization for ‘arr[1].str1[0]’) [enabled by default]
my_strcmp.c:65:9: warning: initialization makes integer from pointer without a cast [enabled by default]
my_strcmp.c:65:9: warning: (near initialization for ‘arr[1].str1[1]’) [enabled by default]
my_strcmp.c:66:9: warning: initialization makes integer from pointer without a cast [enabled by default]
{"string", NULL, 1},
^
my_strcmp.c:66:9: warning: (near initialization for ‘arr[2].str2[0]’) [enabled by default]

我不明白;我在哪里从指针制作整数?请帮忙。

最佳答案

我假设第 64、65 和 66 行是使用 NULL 初始化结构的行。这是行不通的,因为 NULL 是一个指针,您不能用指针初始化数组。

我能看到的唯一解决方案是将结构中的字符串数组转换为指针,或者将“未使用”字符串初始化为空字符串 ""

关于使用结构时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38930245/

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