簡體   English   中英

如何將指針傳遞給C中的多維數組?

[英]How do I pass a pointer to a multidimensional array in C?

我有一個二維數組,可以使用一個函數:

bool matrix[rows][cols];
func(rows, cols, matrix);

void func(int rows, int cols, bool matrix[rows][cols]) {
    //...
}

但是,只要我嘗試將原始函數中的matrix修改為:

bool matrix[rows][cols];
func(rows, cols, &matrix);

void func(int rows, int cols, bool *matrix[rows][cols]) {
    //...
}

我收到一個不兼容的指針類型錯誤。 我對此無能為力。

bool matrix[rows][cols]bool類型的數組數組

bool* matrix[rows][cols]是一個指向bool或簡單bool*的類型指針數組的數組。

因此,如果您將函數定義為采用bool*類型的數組數組,則需要傳遞該類型:

bool* m[row][col];
func( row , col , m );

如果你想要一個指向bool matrix[rows][cols]的指針,那么你的方法是不正確的。
指向矩陣的指針的類型為: bool (*pmatrix)[rows][cols] 因此,使用該類型定義函數並傳遞矩陣數組的地址:

func( rows , cols , &matrix );

@ 2501 已經回答了你的問題 ,但是,既然你想要將修改后的數組反映到main函數中,你實際上並不需要指向數組的指針(這會使事情復雜化)! 只需直接傳遞數組,就可以得到預期的結果!

你為什么問?

簡答:在C中,數組通過引用傳遞。

答案很長:

請記住, 當您使用數組的名稱時,它會轉換為指向其第一個元素 的指針 這通常被稱為“ 陣列衰減 ”。

回到你的代碼, bool matrix[rows][cols];bool matrix[rows][cols]; 將會:

+---------------------+---------------------+---------------------+---------------------+---------------------+
|                     |                     |                     |                     |                     |
|     matrix[0][0]    |     matrix[0][1]    |     matrix[0][2]    |         ...         | matrix[0][cols - 1] |
|                     |                     |                     |                     |                     |
+---------------------+---------------------+---------------------+---------------------+---------------------+
|                     |                     |                     |                     |                     |
|     matrix[1][0]    |     matrix[1][1]    |     matrix[1][2]    |         ...         | matrix[1][cols - 1] |
|                     |                     |                     |                     |                     |
+---------------------+---------------------+---------------------+---------------------+---------------------+
|                     |                     |                     |                     |                     |
|         ...         |         ...         |         ...         |         ...         |         ...         |
|                     |                     |                     |                     |                     |
+---------------------+---------------------+---------------------+---------------------+---------------------+
|                     |                     |                     |                     |                     |
| matrix[rows - 1][0] | matrix[rows - 1][1] | matrix[rows - 1][2] |         ...         | matrix[rows - 1][cols - 1] |
|                     |                     |                     |                     |                     |
+---------------------+---------------------+---------------------+---------------------+---------------------+

從上圖中可以清楚地看到第一個元素

bool matrix[rows][cols];

是第一個子陣列matrix[0][0]matrix[0][cols - 1] 那么這里發生的是這個子陣列的地址被傳遞給函數 這是bool (*)[cols] 這意味着

void func(int rows, int cols, bool matrix[rows][cols])

會像以前一樣工作

void func(int rows, int cols, bool (*matrix)[cols])

因此,例如,如果要寫入matrix的第二個子陣列的第三個槽,可以使用matrix[1][2] = WHATEVER; 並且自該地址通過以來,該函數所做的更改也會影響調用者。


:有一些例外情況,陣列“衰變”不會發生。 請參見異常數組不衰減成指針?

指向單維數組的指針,比如int a[10]可能如下所示:

int (*ptr)[10]
       |    |______________array of 10 integers(read type and number together)
       |______a pointer to ^

指向多維數組的指針,比如int a[10][10]可能如下所示:

int (*ptr)[10][10]
      |    |     |_________________array of 10 integers(read type and number together)
      |    |______________array of ^ 
      |______a pointer to ^

警告:注意括號。

*matrix[rows][cols])不同於(*matrix)[rows][cols]) @ 2501在答案中指出了不同之處。

您可以使用***作為矩陣。

char ***matrix = alloc_matrix(BUFFER_SIZE, BUFFER_SIZE);

char ***alloc_matrix(unsigned rows, unsigned columns) {
    char ***matrix = malloc(rows * sizeof(char **));
    if (!matrix) abort();

    for (unsigned row = 0; row < rows; row++) {
        matrix[row] = calloc(columns, sizeof(char *));
        if (!matrix[row]) abort();
        for (unsigned column = 0; column < columns; column++) {
            matrix[row][column] = NULL;
        }
    }
    return matrix;
}

這是一個使用mallocpointers創建矩陣的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM