C--動態申請二維數組

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

int** malloc2d(int row, int col)
{
    int** ret = NULL;
    
    if( (row > 0) && (col > 0) )
    {
        int* p = NULL;
        
        ret = (int**)malloc(row * sizeof(int*));
        p = (int*)malloc(row * col * sizeof(int));
        
        if( (ret != NULL) && (p != NULL) )
        {
            int i = 0;
            
            for(i=0; i<row; i++)
            {
                ret[i] = p + i * col;
            }
        }
        else
        {
            free(ret);
            free(p);
            
            ret = NULL;
        }
        
    }
    
    return ret;
}

void free2d(int** p)
{
    if( *p != NULL )
    {
        free(*p);
    }
    
    free(p);
}

int main()
{
    int** a = malloc2d(3, 3);//malloc申請的內存需要先清零,不然申請的內存會有亂值。
    int i = 0;
    int j = 0;
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            a[i][j] = i+1;
            printf("%d, ", a[i][j]);
        }
        
        printf("\n");
    }
    
    free2d(a);
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章