【问题标题】:Ways to create dynamic matrix in C?在 C 中创建动态矩阵的方法?
【发布时间】:2012-10-05 13:50:38
【问题描述】:

这是我知道在 C 中动态创建矩阵(二维数组)并将用户输入读取到其元素中的唯一方法:

  • 创建一个指向 x 指针数组的指针,其中每个指针 表示矩阵中的一行 - x 是矩阵中的行数(其高度)。

  • 将这个数组中的每个指针指向一个包含y元素的数组, 其中y 是矩阵中的列数(宽度)。


int main()
{

  int i, j, lines, columns, **intMatrix;

  printf("Type the matrix lines:\t");
  scanf("%d", &lines);
  printf("Type the matrix columns:\t");
  scanf("%d", &columns);

  intMatrix = (int **)malloc(lines * sizeof(int *)); 
  //pointer to an array of [lines] pointers

  for (i = 0; i < lines; ++i)
      intMatrix[i] = (int *)malloc(columns * sizeof(int)); 
      //pointer to a single array with [columns] integers

  for (i = 0; i < lines; ++i)
  {
      for (j = 0; j < columns; ++j)
      {
        printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
        scanf("%d", &intMatrix[i][j]);
      }
  }

还有其他方法吗?

【问题讨论】:

标签: c matrix dynamic-arrays


【解决方案1】:

你可以这样试试

int main()
{    
  int i, j, lines, columns, *intMatrix;

  printf("Type the matrix lines:\t");
  scanf("%d", &lines);
  printf("Type the matrix columns:\t");
  scanf("%d", &columns);

  intMatrix = (int *)malloc(lines * columns * sizeof(int)); 

  for (i = 0; i < lines; ++i)
  {
      for (j = 0; j < columns; ++j)
      {
        printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
        scanf("%d", &intMatrix[i*lines + j]);
      }
  }

【讨论】:

  • 如何在我的示例 (**intMatrix) 和您的示例 (*intMatrix) 中将矩阵传递给函数?如何访问函数内的矩阵元素?谢谢。
  • 您可以将int *matrix传递给函数,然后将元素计算为intMatrix[i*lines + j],如示例所示。
【解决方案2】:

从 C99 开始(但不是 C++),您可以使用可变长度数组:

int main()
{    
  int i, j, lines, columns;

  printf("Type the matrix lines:\t");
  scanf("%d", &lines);
  printf("Type the matrix columns:\t");
  scanf("%d", &columns);

  {
    int intMatrix[lines][columns];

    for (i = 0; i < lines; ++i)
    {
        for (j = 0; j < columns; ++j)
        {
          printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
          scanf("%d", &intMatrix[i][j]);
        }
    }
  }
}

甚至像这样:

void readData (int lines, int columns, int array[lines][columns])
{
  int i, j;

  for (i = 0; i < lines; ++i)
  {
      for (j = 0; j < columns; ++j)
      {
        printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
        scanf("%d", &array[i][j]);
      }
  }
}

int main()
{
  int lines, columns;

  printf("Type the matrix lines:\t");
  scanf("%d", &lines);
  printf("Type the matrix columns:\t");
  scanf("%d", &columns);

  {
    int intMatrix[lines][columns];

    readData (lines, columns, intMatrix);
  }
}

但是,在这两种情况下,数组数据都存储在堆栈中,而不是堆中,因此无法正确存储它,并且不能将其放入 struct 或任何 malloc'd 中。

【讨论】:

  • 现在C99在C编程中流行吗? (我目前正在使用 C85)但我不知道现在真正的 C 程序员使用什么(在行业中)。
  • 有 C85 吗?我知道 C89(又名 C90)、C99 和 C11。真正的代码使用编译器允许的任何东西,无论是标准的还是其他的。 :)
【解决方案3】:
struct matrix {
    type *mem;
};

struct matrix* matrix_new () {
    struct matrix *M = malloc (sizeof(matrix));
    M->mem = malloc (sizeof (type) * rows * cols);
    return M;
}

使用 realloc,memcpy,memmovefree 修改数组的块。要访问单个元素,请使用 mem[row*cols+col]mem[rows*col+row] 之类的内容,具体取决于您的优先级(或您定义的行和列)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2015-11-24
    • 2019-05-22
    • 2018-01-27
    相关资源
    最近更新 更多