c - 动态多维数组

标签 c multidimensional-array malloc

我尝试了一切,据我所知,这段代码是正确的,但它仍然给我的段错误。帮忙?

#include <stdio.h>
#include<malloc.h>

void da(int ***array, int row, int col){
    int i;
    *array=(int **)malloc(sizeof(int *)*row);
    for (i=0; i<row; i++)
        *array[i]=(int *)malloc(sizeof(int)*col);   
}

main(){
    int **array;
    int i,n,m;
    printf("Input number of rows: ");
    scanf("%d",&n);
    printf("Input number of columns: ");
    scanf("%d",&m);
    da(&array,n,m);
    for (i=0; i<n; i++)
        free(array[i]);
    free(array);
}

最佳答案

运算符 [] 比运算符 * 具有更高的优先级。 将括号放在:(*array)[i]=(int *)malloc(sizeof(int)*col);

关于c - 动态多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6612574/

相关文章:

c++ - "each CPU instruction can manipulate 32 bits of data"是什么意思?

php - 从二维数组中删除键的最简单方法?

c++ - 将 3D 数组作为结构成员存储在堆上

C - 使用 malloc 时程序崩溃

c - 在链表中,程序在进入结构体成员时关闭

c - 尝试运行 C 程序时不显示输出

c - 如何使用适当的 GUI 从头开始​​为 QNX 开发应用程序?

C:如何在没有 strtol 的情况下从字符串中提取数字

c - 如何通过 qsort 对 C 中的 3D 字符数组进行排序

c - malloc函数(动态内存分配)导致全局使用时出错