我想创建一个函数来分配(带malloc/ calloc)声明为双指针的矩阵.我理解双指针矩阵如何工作以及如何分配它malloc,但当我传递我的矩阵(声明main()并初始化NULL)时,我的程序崩溃了.我想错误与我的allocMatrix()功能有关,因为如果我在main所有工作中顺利分配矩阵.谢谢 :-)
主要:
#include <stdio.h>
#include <stdlib.h>
#include "Data.h"
#define ROW 5
#define COL 5
int main(void) {
int i,j, ret;
int nRow, nCol;
int **mat=NULL; // double pointer matrix
nRow = 0;
nCol = 0;
//Insert n of row and columns
printf("Insert n of rows and columns:\n");
scanf("%d %d", &nRow, &nCol);
//Functions to allocate matrix
ret=allocMatrix(mat, nRow, nCol);
printf("Return value: %d\n",ret);
/*
this code works …Run Code Online (Sandbox Code Playgroud)