Dynamic allocation of 2D array within function (using pointers to return adress of allocated object)

后端 未结 2 1886
悲&欢浪女
悲&欢浪女 2020-12-20 08:35

I\'d /ike to know, how to pass pointers to dynamically allocated arrays using function arguments. This function is supposed to allocate array 10x10 (checks skipped for simpl

2条回答
  •  Happy的楠姐
    2020-12-20 09:19

    I came across this post when I was facing a similar problem (I was looking for a way to dynamically allocate an array of strings in C). I prefer to return the array pointer from the function. The following worked for me (I adapted it for your array of integers). I arbitrarily set 99 for each value so I could see them printed out in main.

    int **array_allocate2DArray(unsigned int size_x, unsigned int size_y)
    {
            int i;
            int **arr;
    
            arr = malloc(size_x*(sizeof(int*)));
    
            for (i=0 ; i

提交回复
热议问题