簡體   English   中英

如何在 C 中通過指針傳遞二維數組?

[英]How to pass a 2D array by pointer in C?

我正在學習 C 並且無法將二維數組的指針傳遞給另一個 function 然后打印二維數組。 任何幫助,將不勝感激。

int main( void ){
    char array[50][50];
    int SIZE;

    ...call function to fill array... this part works.

    printarray( array, SIZE );
}

void printarray( char **array, int SIZE ){
    int i;
    int j;

    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", array[j][i] );
        }
        printf( "\n" );
    }
}

char **不代表二維數組 - 它是指向指針的指針數組。 如果要傳遞二維數組,則需要更改printarray的定義:

void printarray( char (*array)[50], int SIZE )

或等效地:

void printarray( char array[][50], int SIZE )

main() ,變量“array”被聲明為

char array[50][50];

這是一個 2500 字節的數據。 當傳遞main()的“數組”時,它是指向該數據開頭的指針。 它是一個指向預期以 50 行組織的字符的指針。

然而在函數printarray() ,你聲明

 char **array

這里的“數組”是一個指向char *pointer

@Lucus 對void printarray( char array[][50], int SIZE )有效,但它不是通用的,因為您的 SIZE 參數必須為 50。

思路:打敗(yeech) printarray()中參數數組的類型

void printarray(void *array, int SIZE ){
    int i;
    int j;
    char *charArray = (char *) array;

    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", charArray[j*SIZE + i] );
        }
        printf( "\n" );
    }
}

一個更優雅的解決方案是使main()的“數組”成為一個指針數組。

// Your original printarray()
void printarray(char **array, int SIZE ){
    int i;
    int j;
    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", array[j][i] );
        }
        printf( "\n" );
    }
}

// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
  // Note: cleaner alternative syntax
  // array = malloc(sizeof *array * SIZE);
// Allocate rows
for (int row = 0; row<SIZE; row++) {
  // Note: sizeof(char) is 1. (@Carl Norum)
  // Shown here to help show difference between this malloc() and the above one.
  array[row] = (char *) malloc(SIZE * sizeof(char));
    // Note: cleaner alternative syntax
    // array[row] = malloc(sizeof(**array) * SIZE);
  }
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
  for (int col = 0; col<SIZE; col++) {
    array[row][col] = 'a';  // or whatever value you want
  }
}
// Print it
printarray(array, SIZE);
...

由於 C99 支持動態大小的數組,下面的樣式只是更方便地傳遞一個 2 維數組:

void printarray( void *array0, int SIZE ){
    char (*array)[SIZE] = array0;
    int i;
    int j;
    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", array[j][i] );
        }
        printf( "\n" );
    }
}

這里沒有一個答案是我要找的,所以我發布了我對問題的簡單解決方案

#include <iostream>

using namespace std;

void example(int* mat, int dim0, int dim1){
    
    for(int i = 0; i < dim0; ++i) {
         for(int j = 0; j < dim1; ++j) {
             auto cur_index = i * dim1 + j;
            cout<< *(mat + cur_index) << endl;
        }
    }
}

int main()
{
    const int dim0 = 3;
    const int dim1 = 2;

    int mat[dim0][dim1];
    
    for(int i = 0; i < dim0; ++i) {
         for(int j = 0; j < dim1; ++j) {
            mat[i][j] = i * dim1 + j;
        }
    }
    
    example(&(mat[0][0]), dim0, dim1);
    return 0;
}

您可以使用雙指針輕松傳遞二維數組。

  void printarray( char **array, int n)
  {
     int i, j;
     for(i=0; i<n; i++ )
     {
         for(j=0; j<n; j++)
         {
            printf("%c ", array[i][j] );
         }
        printf( "\n" );
     }
  }

  int main()
  {
      int n = 2;
      int i, j;

      char **array = (char **) malloc(n * sizeof(char*));

      for (i=0; i<n; i++) 
      {
        array[i] = (char *) malloc(n* sizeof(char));
      }

     for (i=0; i<n; i++)
     {
       for (j=0; j<n; j++)
       {
           scanf("%c ", &array[i][j]);
       }
     }

     printarray(array, n);

     return 0;
  }

完整代碼: Ideone

暫無
暫無

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

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