我嘗試了一切,並從我的理解,這段代碼是正確的,但它仍然給我的分段錯誤。幫幫我?Dynamic Multidimensional Arrays
#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);
}
它在哪裏段錯誤?你到目前爲止嘗試解決這個問題? –
請勿施放'malloc'的返回值。鑄造充其量是多餘的,並且(**和你的代碼**一樣)可能會隱藏錯誤; *即沒有聲明'malloc'的頭文件,使得編譯器假定返回類型是'int'而不是'void *'。* – pmg