如何使用 void 函数初始化二维数组?

3no*_*tur 2 c pointers function multidimensional-array

我想通过使用简单的 void 函数“assignTable”将 2D 字符数组“table”初始化为两个预定义的 2D 字符数组(称为“A”和“B”)之一。然而,虽然数组在“assignTable”中获得了正确的值,但分配的值似乎没有转移到主函数中。我怀疑指针有问题。

你能告诉我我做错了什么吗?

#include <stdio.h>
#include <stdlib.h>


char A[10][10] = {
        {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
        {'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
        {'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
        {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
        {'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
        {'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
        {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
        {'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
        {'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
        {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}
};

char B[10][10] = {
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
        {' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'}
};

void printTable(int rows, int columns, char table[rows][columns])
{
  for (int i = 0; i < rows; i = i + 1)
    {
      for (int j = 0; j < columns; j = j + 1)
          printf("%c", table[i][j]);
      printf("\n");
    }
  printf("\n");
}

void asssignTable(char* table, char* table_identity)
{
  if (table_identity[0] == 'A')
    table = A;
  else if (table_identity[0] == 'B')
    table = B;
  printf("In the function ""assignTable"":\n");      // does work 
  printTable(10, 10, table);
}

int main()
{
  char (*table)[10];
  asssignTable(&table, "A");
  printf("In main :\n");          // does not work
  printTable(10, 10, table);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Lun*_*din 5

必备知识:

  • char*char(*)[columns]与或 没有任何共同点char [rows][columns],因此不能使用。
  • char(*)[columns]是一个指向一维数组的指针。尽管它可以在迭代二维数组时使用。
  • 每当将数组传递给函数或将其声明为函数参数时,它都会“衰减”为指向其第一个元素的指针。的第一个元素char table[rows][columns]char [columns]类型。指向此类类型的指针是char (*)[columns]
  • 每当我们将某些内容分配给函数内的(指针)参数时,该更改不会影响调用方的指针。我们必须为指针参数所指向的内容分配一些内容

解决方法是重写该函数,如下所示:

void asssignTable(int rows, 
                  int columns, 
                  char (**table)[rows][columns], 
                  const char* table_identity)
{
  if (table_identity[0] == 'A')
    *table = &A;
  else if (table_identity[0] == 'B')
    *table = &B;
  printf("In the function ""assignTable"":\n");
  printTable(rows, columns, **table);
}
Run Code Online (Sandbox Code Playgroud)
  • rows因为columns我们希望使用与您的printTable函数一致的 API。
  • 我们不能使用 achar table [rows][columns]因为它会衰减为 achar (*)[columns]并且将该指针分配给某个对象不会影响调用者。我们必须使用一个指向指针的指针——在本例中是一个指向二维数组的指针。(这种语法与普通指针相当一致。)
  • *table取消引用指针到指针并让我们可以访问 main() 中使用的指针。&A我们将此指针设置为指向二维数组等的地址。
  • printTable(rows, columns, **table);通过取消引用两次,我们得到一个数组char [rows][columns],之后它将像往常一样衰减为指向第一个元素的指针,因为我们永远无法真正按值将数组传递给函数。

调用者代码变为:

char (*table)[10][10];
asssignTable(10, 10, &table, "A");
printf("In main :\n");
printTable(10, 10, *table);
Run Code Online (Sandbox Code Playgroud)