malloc-ating multidimensional array in function

前端 未结 7 1843
傲寒
傲寒 2020-12-10 22:28

I\'m trying to allocate a 2d array in a C program. It works fine in the main function like this (as explained here):

#include 
#include 

        
7条回答
  •  悲&欢浪女
    2020-12-10 23:19

    That's not your about your segmentation fault problem, but you should really consider to use a single malloc call to allocate all necessary memory space need by grid.

    grid = malloc (nrows * ncols * sizeof(int *))
    

    Look at Bill ONeary answer regarding pointer of pointer of pointer.

提交回复
热议问题