简体   繁体   English

Struct中的2d数组-C-

[英]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: 使用memcpyarr的内容复制到其中,或将其类型更改为指针:

typedef struct {
    char character;
    int width;
    int (*charData)[5];
} Letter;

Also, 也,

  1. Identifier names shouldn't start with _ followed by a capital 标识符名称不应以_开头,后跟大写字母
  2. NULL should only be used for pointers; NULL仅应用于指针; use '\\0' for characters, plain 0 for integers 使用'\\0'表示字符,以0表示整数
  3. You don't check the return value of malloc for null 您无需检查malloc的返回值是否为null
  4. You're not freeing allocated memory. 您没有释放分配的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM