Я хочу создать функцию для выделения (с malloc/calloc) матрицы, объявленной как двойной указатель. Я понял, как работает матрица двойных указателей и как распределить ее с помощью malloc, но когда я передаю свою матрицу (объявленную в main() и инициализированную до NULL), моя программа сработает. Я полагаю, что ошибка связана с моей функцией allocMatrix(), потому что, если я выделяю матрицу в main, все работает плавно. Спасибо: -)
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 perfect!
    mat= malloc(nRow * sizeof(int));
    i=0;
    while( i < nRow)
    {
        mat[i]=malloc(nCol * sizeof(int));
        i++;
    }
    */
    //Get Values from stdin
    i=0;
    while( i < nRow)
    {
        j=0;
        while (j < nCol)
        {
            printf("Insert value pos[%d,%d]:\n", i, j);
            scanf("%d", &mat[i][j]);
            j++;
        }
        i++;
    }
    //Print values
    i=0;
    while (i < nRow)
    {
        j=0;
        while( j < nCol)
        {
            printf("Value pos[%d,%d] is: %d \n", i, j, mat[i][j]);
            j++;
        }
        i++;
    }
    system("pause");
    return EXIT_SUCCESS;
}
 allocateMatrix:
int allocMatrix(int **matrix, int nRow, int nCol)
{
    int i;
    int ext_status;
    //Classic allocation method for matrix
    matrix= malloc( nRow * sizeof(int));
    if ( matrix != NULL)
    {
        i=0;
        while (i < nRow)
        {
            matrix[i]= malloc(nCol * sizeof(int));
            if( matrix[i] != NULL)
                ext_status= 1;
            else
                ext_status= 0;
            i++;
        }
    }
    else
        ext_status= 0;
    return ext_status;
 }
