[英]C Data structure how to declare
I am stuck on the on how to manipulate the data structure. 我坚持如何操纵数据结构。
I have header file that declare like this 我有这样声明的头文件
struct item{
int i;
char str[88];
};
and I have a C file that I want to make 9 structure items (I declare as global variable and I already include the header file): 并且我有一个要制作9个结构项的C文件(我声明为全局变量,并且已经包含了头文件):
struct item a[9];
but when I want to put the data that I want into 但是当我想要将想要的数据放入时
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;
...
}
but when ever I tried to compile it seem that it always shows 但是当我尝试编译时,它似乎总是显示
error: expected an identifiler
a[item_index].str = string;
This line will not behave the way you expect it to. 这条线不会像您期望的那样运行。 You would need to use
strcpy()
in order to copy strings: 您将需要使用
strcpy()
来复制字符串:
strcpy(a[item_index].str, string)
Array Name is a non modifiable (read only) variable or better say constant. 数组名称是不可修改的(只读)变量,或者最好是常量。
In this statement: 在此语句中:
a[item_index].str = string;
you try to modify an array str
, it is not permissible. 您尝试修改数组
str
,这是不允许的。
Either you can assign value to each index one by one (that is what done by strcpy
), 您可以一个一个地为每个索引分配值(即由
strcpy
完成),
Or declare a pointer *str
instead of array and then assign it any address you want. 或声明一个指针
*str
而不是数组,然后为其分配所需的任何地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.