2013-02-19 32 views
0

我已經閱讀了關於傳遞指向多維數組的指針的一些材料,但是我無法讓它爲我自己工作。如何將指針傳遞給抽象數據類型的多維數組

我有:

/* This code basically, in order, does this--or tries to: 
    - Create 2D array of cell structs 
    - Create info_pass detailing certain attributes of the 2D array 
     + in particular, I am trying to include a pointer to the 2D array so that I 
      can pass the info_pass struct between functions and update the contents of 
      the 2D array in each function. 
    - The updating is done in struct info_pass* update(...){} 
    - ... however, in my full program, there are several other functions it is passed 
     to, so being able to pass a pointer that allows manipulation of the 2D array is 
     what I'm really after. 
*/ 

struct info_pass { 
    /* stuff */ 
    struct cell* master; 
}; 
struct cell { 
    /* values */ 
    /* lots of pointers to other cells */ 
}; 
struct info_pass* genesis() {     /* creating an the multiD array */ 
    /* stuff */ 
    struct cell* (*cells)[width]; 
    cells = malloc(sizeof(struct cell) * width * length); 

    struct info_pass* keycard = NULL; 
    keycard = malloc(sizeof(struct info_pass)); 
    /* assign values to key card */ 
    keycard->master = cells;  /* problem here?! */ <==== (A) 

    /* update cells */ 

    return keycard;       /* therefore problem here too */ 
} 
struct info_pass* update(struct info_pass* key) { 
    struct info_pass* keyRef = NULL; 
    keyRef = malloc(sizeof(struct info_pass)); 
    keyRef = key;        /* and of course here */ 

    struct cell* home1 = NULL; 
    home1 = malloc(sizeof(struct cell)); 

    /*here I want to update the multidimensional array*/ <===== (B) 
    /*... and then send it back ...*/ 
    return keyRef; 
} 

錯誤@(A) =警告:分配從兼容的指針類型。

錯誤@(B) =錯誤:下標值既不是數組也不是指針。

只是希望推向正確的方向。

編輯

按ThePosey的建議,我會表現出更多的參與「錯誤代碼:下標值既不是指針也不array`s。我將在下面添加它,而不是將它放到上面的代碼示例中,以便爲將來的上下文保留原始問題的狀態。

struct info_pass* update(struct info_pass* key) {  

    /* passing data, including a pointer to a 2D array from info_pass  */ 
    /* struct then I want to access the 2D array and change it's contents */ 
    /* contents and then send it back in another info_pass struct   */ 

    struct info_pass* keyRef = NULL;  
    keyRef = malloc(sizeof(struct info_pass)); 
    keyRef = key;      /* to pass the info back afterwards */ 

    int len = keyRef->length; 
    int wid = keyRef->width; 

    struct cell* home1 = NULL; 
    home1 = malloc(sizeof(struct cell)); 
    home1 = key->masterRef[len][wid];  /* to access and change the data */ 

    int fate = 0; 
    int a = 0; 
    int b = 0; 

    for (a = 0; a < len; a++) { 
      for (b = 0; b < wid; b++) { 
        if (keyRef->masterRef[a][b].go_up.state == 1) { 
    /* just trying different styles of calls */ 
          fate++; 
        } if (home1[a][b].go_down.state == 1) { 
          fate++; 
        } if (home1[a][b]->go_left->state == 1) { 
          fate++; 
        } if (home1[a][b]->go_right->state == 1) { 
          fate++; 
    /* there more calls to the array, and all generate the same error: */ 
    /* subscripted value is neither array nor pointer */ 

回答

1

你在@A錯誤是試圖將cell***分配給cell*。如果你想創建一個多維(從代碼它看起來像你想有一個2D長x寬)陣列,你會做到以下幾點:

struct cell* cells[length]; 

for (int i = 0; i < length; i++) 
{ 
    //give each row width number of cell structs 
    cells[i] = malloc(sizeof(struct cell) * width); 
} 

試圖幫助你的問題的其餘部分。你會改變

struct info_pass { 
    /* stuff */ 
    struct cell* master; 
}; 

struct info_pass { 
    /* stuff */ 
    struct cell** master; 
}; 

,但你可能還需要保持的長度和寬度信息在結構,以及讓你知道數組的大小。之後,只要您有一個信息傳給你可以做一些像訪問獨立的電池元件:

struct cell* single_cell = &my_info_pass->master[lengthIndex][widthIndex]; 

,或者如果你有一個像在例如細胞結構一個單元ID INT直接獲取值:

int cell_value = my_info_pass->master[lengthIndex][widthIndex].cell_id; 

沒有一個更具體的案例和確切的代碼,很難縮小你不理解的部分。希望這會有所幫助。

+0

它確實是一個二維數組!你的創建方法是否可以更容易地將指針傳遞給它?我不明白它將如何彌補這一困難。我將用簡要的解釋來編輯OP。 – d0rmLife 2013-02-19 03:17:48

+0

「通過」是一個與函數參數相關的動詞。將一個指針傳遞給像數組這樣的數據指針可能意味着什麼? – 2013-02-19 03:29:34

+0

@RonBurk指針指向這條數據,以便數據可以被操縱 - 它不是*參數。類似於鏈表。 – d0rmLife 2013-02-19 03:40:36

1

不是一個真正的答案,而是一個需要格式化的筆記。 :-)理解C中的「數組」只是指針算術,這很方便。例如:

char* ptr = "abcd"; 

    printf("Letter = %c\n", ptr[1]); 
    printf("Letter = %c\n", 1[ptr]); // Same damn thing! 
    printf("Letter = %c\n", *(1 + ptr)); // and again! 

所以,當你在做什麼的樣子「數組索引」給你,C是剛加入的東西,並通過他們indirecting。語法「x [y]」表示「將x添加到y並將結果用作指針」。 (當然,要注意的一點是,C將整數乘以指針所指向的東西的大小,然後再將它們添加到指針中)

IOW,[]運算符確實意味着「添加和間接」。

好的舊ANSI C是否有多維數組?並非如此,並不是說像FORTRAN這樣的使用它們的語言確實如此。但是,只要你有簡單的數組和指針算術,你可以推出你自己的。所以,如果我想要一個一維數組,我只需要一個指向malloc()提供的內存的指針。但是如果我想要一個二維數組,那麼我需要一個指針數組,每個指針指向malloc()返回的一些內存。因爲這樣的:

int** Matrix = MallocMatrix(3, 5); 

Matrix[2][3] = 0; 

意思是 「添加2 *的sizeof(INT *)以矩陣和間接然後添加3 *的sizeof(int)以該和間接。」