0

I am struggling with function int** Solve(). I did not find solution anywhere, or maybe I can not express my problem accuratelly. I am unable return right value, I always get compilator error or segmentation fault. How to initialize int ptr**, which I want to return from function? Note that function type (int**) is given, and I am not able to change it. I tried to use calloc, because if I wanted to return something line int ptr[4][4], it said I am returning local variable. I don't know what to do, and ptr does not have to be int** maybe. I just want to return right elements and using return ptr. My code:

int** Solve(){
    int array[6][6];

    //some determining of array's elements...

    int **ptr=calloc(4,sizeof(int*));
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            ptr[i][j]=array[i+1][j+1];
        }
    }

    return ptr;
}
victory
  • 185
  • 1
  • 2
  • 16
  • 4
    `int**` would be a staggered-array instead of a multi-dimensional array. Your `calloc` call allocates an array-of-pointers but you don't allocate those sub-arrays. Note that staggered-arrays in C are a bad idea unless you really need a "non-rectangular-sized" buffer because of increased memory fragmentation risk and needing to `free` all of the child arrays. Returning `int**` by itself without indicating the length of the buffer means that consumers won't be able to `free` it properly. – Dai May 30 '18 at 11:40
  • That has been a while since i've done som C but, i feel like you are messing up your **ptr, you are allowing him 4 pointers on int `int*` but those are not allocated themselves to receive ints, you also need to allcoate space for wichever number of ints you need in those pointers. – N.K May 30 '18 at 11:41
  • 2
    See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin May 30 '18 at 11:49
  • @AugustKarlstrom If I create 2d array just by initializing, I can not return it, because is is considered as local variable. And also, int **solve() is given to me, I can not change it. If you meant something different, please try to share example code to let me better understand. Thank you :) – victory May 30 '18 at 19:17
  • @victory Never mind, I was wrong. In C you cannot define a function which takes a two-dimensional array of any size (in both dimensions). – August Karlstrom May 30 '18 at 20:33

1 Answers1

1

I would add a comment under your question, but I lack reputation.

int r = 3, c = 4, i, j, count;

int **arr = (int **)malloc(r * sizeof(int *));
for (i=0; i<r; i++)
     arr[i] = (int *)malloc(c * sizeof(int));

That's how 2d array allocation must look like.

The difference between malloc and calloc is that calloc zero-initializes allocated memory, so they are interchangeable in your case.

Eugene Mart
  • 185
  • 1
  • 10
  • 1
    This is not a 2D array. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin May 30 '18 at 11:50
  • Solved the issue. Could you also clarify, why does r=3, and c=4? How should it be, if I used 4x4 2D array? – victory May 30 '18 at 12:16
  • 1
    r and c are row and column :p – Eugene Mart May 30 '18 at 17:07