[英]Accesing values on a 2d struct array
所以我有這個結構
struct cell
{
int downwall;
int rightwall;
};
我已經為結構單元的二維數組(結構單元**數組)動態分配了內存
但是,當我嘗試使用命令訪問某個單元格時
array[i][j] -> downwall = 0;
我收到此錯誤:
'->' 的無效類型參數(有 'struct cell')
用
array[i][j].downwall = 0;
反而。
你會使用->
if arrray[i][j]
有它沒有的類型struct cell*
。 它的類型為struct cell
。
array[i][j]
將是struct cell
,而不是struct cell *
。 您應該使用.
操作員訪問成員。
你需要寫
array[i][j].downwall = 0; // use of .
請注意
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;
您需要聲明一個具有正確索引數量的實際數組,然后使指針指向它。 使用鍵入的名稱來幫助(簡化匈牙利符號)
int iAry[M][N];
int **ptrAry;
ptrAry = iAry; /* equivalent to ptrAry = &iAry[0][0]; */
/* then use -> operator as you have done */
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.