[英]2d array in Struct - C -
Im trying to assign a array to my letter.charData but i get this error: 我试图给我的letter.charData分配一个数组,但出现此错误:
IntelliSense: expression must be a modifiable lvalue IntelliSense:表达式必须是可修改的左值
Im trying to add my array arr to letter.charData 我正在尝试将我的数组arr添加到letter.charData
Thanks in advance! 提前致谢!
struct _Letter{
char character;
int width;
int charData[8][5];
};
typedef struct _Letter Letter;
Letter *allocLetter(void)
{
Letter *letter;
letter = (Letter*) malloc(1 * sizeof(Letter));
letter->character = NULL;
letter->width = NULL;
/* charData? */
return letter;
}
int main(void)
{
Letter letter = *allocLetter();
int arr[8][5] =
{
0,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0
};
letter.character = '1';
letter.charData = arr;
return(0);
}
_Letter::charData
is an array, not a pointer, so you can't just assign another array to it. _Letter::charData
是一个数组,而不是一个指针,因此您不能只是为其分配另一个数组。 Either copy arr
's contents into it with memcpy
, or change its type to a pointer: 使用
memcpy
将arr
的内容复制到其中,或将其类型更改为指针:
typedef struct {
char character;
int width;
int (*charData)[5];
} Letter;
Also, 也,
_
followed by a capital _
开头,后跟大写字母 NULL
should only be used for pointers; NULL
仅应用于指针; use '\\0'
for characters, plain 0
for integers '\\0'
表示字符,以0
表示整数 malloc
for null malloc
的返回值是否为null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.