定义后的C数组多赋值不可能
我在问:是否可以在数组定义后执行多个赋值?我用不同的语法进行了不同的测试,但没有成功。
这是我的源代码:
#include <stdio.h>
typedef int coords[3];
int main(int argc, char **argv)
{
coords x = {1, 2, 3}; // it works
x = {1, 2, 3}; // it doesn't work. Compiler gives the 'error: expected expression before ‘{’ token'
// x = (int[3]) {1, 2, 3}; // it doesn't work. Compiler gives the 'error: assignment to expression with array type'
// x = (coords[3]) {1, 2, 3};// it doesn't work. Compiler gives the 'error: assignment to expression with array type'
// x = (coords) {1, 2, 3}; // it doesn't work. Compiler gives the 'error: assignment to expression with array type'
printf("%dn", x[0]);
return 0;
}
我错在哪里?
谢谢
马可
回答
是否可以在数组定义后执行多个赋值?
不。
只有在初始化期间,您才能为多个数组元素赋值。
只能对单个数组元素进行赋值。