gpt4 book ai didi

C数据结构如何声明

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

我被困在如何操作数据结构上。

我有这样声明的头文件

struct item{
int i;
char str[88];
};

我有一个 C 文件,我想制作 9 个结构项(我声明为全局变量并且我已经包含头文件):

struct item a[9];

但是当我想把我想要的数据放进去的时候

foo()

{
...
// let's say I have data int in index and char[] in string
// and I want it to put at item_index

a[item_index].i = index;
a[item_index].str = string;
...
}

但是当我尝试编译时,它似乎总是显示

error: expected an identifiler

最佳答案

a[item_index].str = string;

此行不会按您预期的方式运行。您需要使用 strcpy() 来复制字符串:

strcpy(a[item_index].str, string)

关于C数据结构如何声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19594931/

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