小编the*_*sch的帖子

C - 使用malloc创建一个数组,然后遍历它

我正在学习C并尝试编写此功能

int *create_matrix(int n) {
   int *matrix = malloc(n*n*sizeof(int));
   srand(time(NULL));
   int i, j;
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        matrix[i][j] = rand()%10;
      }
   }
   return matrix;
}
Run Code Online (Sandbox Code Playgroud)

为什么这不能编译?它的抱怨matrix[i][j]不是指针/数组.但我刚刚宣布它为指针六行以上......

c arrays malloc pointers loops

0
推荐指数
1
解决办法
2366
查看次数

标签 统计

arrays ×1

c ×1

loops ×1

malloc ×1

pointers ×1