小编CSD*_*ude的帖子

动态分配矩阵的功能

我想创建一个函数来分配(带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)

c malloc pointers dynamic matrix

3
推荐指数
2
解决办法
5363
查看次数

标签 统计

c ×1

dynamic ×1

malloc ×1

matrix ×1

pointers ×1