【问题标题】:Set new value to complete row of 2-dimensional array in C在C中设置新值以完成二维数组的行
【发布时间】:2020-10-17 12:17:25
【问题描述】:

我尝试在运行时将新值设置为完整的行:


#definde ROW1  {1, 0}

int seq[5][2];

void test(void)
{
 seq[0] = ROW1;
}

Compiling error: 
expected expression before '{' token
#define ROW1   {1, 0}

请帮帮我! 非常感谢!

【问题讨论】:

  • 数组不可赋值,因为你在 C 中,这包括数组数组中的数组(这就是 seq 是什么。你需要通过直接赋值来设置每个元素(通常带循环),或大容量内存副本(如果适合您的特定上下文)。

标签: c multidimensional-array


【解决方案1】:

你不能分配你需要复制它们的数组——例如:

#define ROW1  {1, 0}
#define ASSIGN(arr, init, type) memcpy(arr, (type[])init, sizeof((type[])init))

int seq[5][2];

void test(void)
{
     ASSIGN(seq[0], ROW1, int);
}

你也可以初始化它:

int seq[5][2] = {[0] = ROW1};

【讨论】:

    猜你喜欢
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多