C语言 如何通过C中的指针传递二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16724368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:26:50  来源:igfitidea点击:

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

carrayspointers

提问by user1362058

I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.

我正在学习 C 并且在将 2D 数组的指针传递给另一个然后打印 2D 数组的函数时遇到了问题。任何帮助,将不胜感激。

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" );
    }
}

采纳答案by Carl Norum

char **doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarrayif you want to pass it a 2D array:

char **不代表二维数组 - 它是指向指针的指针数组。printarray如果要传递二维数组,则需要更改定义:

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

or equivalently:

或等效地:

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

回答by chux - Reinstate Monica

In main(), the variable "array" is declared as

在 中main(),变量“数组”被声明为

char array[50][50];

This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.

这是一个 2500 字节的数据。当main()的“数组”被传递时,它是一个指向该数据开头的指针。它是一个指向预期以 50 行组织的字符的指针。

Yet in function printarray(), you declare

然而在函数中printarray(),你声明

 char **array

"array" here is a pointer to a char *pointer.

这里的“数组”是一个指向 a 的指针char *pointer

@Lucus suggestion of void printarray( char array[][50], int SIZE )works, except that it is not generic in that your SIZE parameter mustbe 50.

@Lucus 对void printarray( char array[][50], int SIZE )作品的建议,除了它不是通用的,因为您的 SIZE 参数必须为 50。

Idea: defeat (yeech) the type of parameter array in printarray()

思路:击败(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" );
    }
}

A more elegant solution is to make the "array" in main()an array of pointers.

一个更优雅的解决方案是在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: alternative syntax
  // array = (char **) malloc(SIZE * sizeof(*array));
// 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: alternative syntax
    // array[row] = (char *) malloc(SIZE * sizeof(**array));
  }
// 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);
...

回答by Simon Woo

Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:

由于 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" );
    }
}

回答by rashedcs

You can easily pass the 2d array using double pointer.

您可以使用双指针轻松传递二维数组。

  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;
  }

Full Code : Ideone

完整代码:Ideone