gpt4 book ai didi

c - Struct fl 没有名为 sub 的成员

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

#include <stdio.h>
#include <stdlib.h>

struct fl{
char sub[3] = {"Math","Science","ICT"};
};

int main()
{
int i;

struct fl floatp;

for (i = 0; i < 3; ++i){
printf (" %s",floatp.sub[i]);
}

return 0;
}

我在第 11 行收到此错误“struct fl 没有名为 sub 的成员”。但我确实有一个名为“sub”的成员。我做错了什么?

最佳答案

你想要这个:

#include <stdio.h>
#include <stdlib.h>

struct fl {
char *sub[3];
};

int main()
{
int i;

struct fl floatp = {{ "Math","Science","ICT" }};
// or if your compiler supports it:
// struct fl floatp = {.sub = { "Math","Science","ICT" }};

for (i = 0; i < 3; ++i) {
printf(" %s", floatp.sub[i]);
}

return 0;
}

关于c - Struct fl 没有名为 sub 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40159280/

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