所以我有這個結構Accesing值
struct cell
{
int downwall;
int rightwall;
};
我已動態分配存儲器,用於結構單元 的2D陣列(結構單元**陣列)
然而,當我嘗試訪問某小區,其命令
array[i][j] -> downwall = 0;
我得到這個錯誤:
invalid type argument of '->' (have 'struct cell')
所以我有這個結構Accesing值
struct cell
{
int downwall;
int rightwall;
};
我已動態分配存儲器,用於結構單元 的2D陣列(結構單元**陣列)
然而,當我嘗試訪問某小區,其命令
array[i][j] -> downwall = 0;
我得到這個錯誤:
invalid type argument of '->' (have 'struct cell')
使用
array[i][j].downwall = 0;
代替。
如果arrray[i][j]
的型號爲struct cell*
,您將會使用->
。它有型號struct cell
。
array[i][j]
的類型將是struct cell
而不是struct cell *
。您應該使用.
運營商訪問成員。
你需要寫
array[i][j].downwall = 0; // use of .
您需要聲明一個具有正確數目的索引的實際數組,然後使指針指向它。使用類型名稱來幫助(簡體匈牙利表示法)
int iAry[M][N];
int **ptrAry;
ptrAry = iAry; /* equivalent to ptrAry = &iAry[0][0]; */
/* then use -> operator as you have done */
請注意,
struct cell** array
不是一個二維數組!它是一個指向'struct cell'類型指針的指針。 只有存在值指向的分配內存(靜態或動態)時,才應將其視爲二維數組。否則,你正在分割故障的道路上。
你的結構是不是指針結構如此簡單做這樣的事情:
//array cells of type cell and can hold 10 cell's
struct cell cells[10];
//Try using malloc for memory allocation
cells[0] = (struct cell *) malloc(sizeof(struct cell));
//example assignment
cells[0].downwall=0;
cells[0].rightwall=1;
'結構單元** array'是** **不是一個二維數組!指針不是數組。改爲使用正確的2D數組。讀一本關於數組,指針和'struct'的C書可能是一個好主意。錯誤信息非常清晰。 – Olaf
在指針聲明中使用雙星似乎是主要問題。你應該使用'struct cell * array = malloc(m * n * sizeof(struct cell))'或者更好地使用'struct cell array [m] [n]'。然後,你可以使用'(array + i * n + j) - > downwall'或'a [i] [j] .downwall'。 – ssd