【发布时间】:2013-10-26 23:15:14
【问题描述】:
我不能做 tab[i] = "return of my function" 吗?
void creat_tab(int x, int y)
{
int tab[x][y];
int i;
int tmp;
tmp = y * 2;
i = 0;
while (i <= x)
{
if (i == 0 || i == x)
tab[i] = place_full_border(tab[i], x, y); /* here */
else if (i == 1)
tab[i] = place_first_wall(tab[i], x, y); /* here */
else
tab[i] = place_wall(tab[i], tab[i - 1], tmp, y); /* here */
i++;
}
aff_tab(tab);
}
当我编译时它告诉我:
从“int *”类型分配给类型“int [(long unsigned int)(y)]”时不兼容的类型
【问题讨论】:
-
数组到指针衰减的结果不是左值,赋值给它没有意义。你不能在 C 中按值传递数组,也没有数组的赋值。
-
@Kerrek SB 你会说下面的
=不是赋值int a[2][3] = {{1,2,3}, {4,5,6}};吗? -
@Kerrek SB 历史注释:Pre C99 允许分配数组,如
typedef struct abc { int tab[3]; } def; def fred(void) { static def d3 = {{1,2,3}}; return d3; } int sally(void) { int ghi[3]; ghi = fred().tab; return ghi[2]; } -
@chux:你仍然可以这样做。数组作为结构成员很好。但我说的是“你不能分配数组”,而不是“你不能分配结构”......
-
@Kerrek SB
fred()返回abc类型的结构。fred().tab是一个数组(int [3]类型)。ghi = fred().tab是数组赋值,不是结构赋值。