【发布时间】:2014-02-23 20:13:32
【问题描述】:
这段代码是我写的
#include <stdio.h>
struct foo
{
int foo1[3];
};
int main(void)
{
int a[] = {1, 2, 3};
struct foo test;
test.foo1 = a;
printf("%d\n", test.foo1[0]);
return 0;
}
它给出编译错误,说它不能将 int * 转换为 int[3]。
我知道数组名称会衰减为表达式中的指针,但有没有办法抑制这种情况,因为我确实需要一个数组?
【问题讨论】:
-
test.foo1=a;到memcpy(test.foo1, a, sizeof(test.foo1)); -
你可以写
struct foo test = { {1,2,3} };