1

The function should get as an argument 2-dimension square array of int.so the function signature :

void f (int** arr,int n) // n is a size of each dimension

I have defined the array :

int arr [][5] = {{0,1,1,1,1},{1,1,1,1,1},{0,1,1,0,1},{0,1,1,1,1},{0,1,1,1,0}}

The call :func (arr,5); //<- compilation error

also func ((int**) arr,5); //<- runtime error (*arr == NULL) how should i sole that?

YAKOVM
  • 9,805
  • 31
  • 116
  • 217

3 Answers3

3

There is slight difference between int** and what you declare, that is why you can not cast them to each other.

This should work for you:

void f (int arr[][5],int n) 

The thing is that when you write arr[i][j] in the body of the function the two indices would need to be translated to a pointer to the memory. For this reason you need the second dimension as the calculation will always be arr[i][j] = arr + i * dimen + j.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
1

The function is declared to take a jagged array, i.e. an array that could have different sizes in each of its rows.

void f (int** arr,int n)

Such an array needs to be created differently - you need to allocate n pointers, and then allocate an individual memory block for each of the array rows:

int **arr = malloc(sizeof(int*)*n);
for (int i = 0; i != n ; i++) {
    // This code uses the same size for each row, but jagged arrays
    // are free of the "rectangular array" limitations.
    arr[i] = malloc(sizeof(int)*n);
}

Once you use such an array, free its rows before freeing the array itself:

for (int i = 0; i != n ; i++) {
    free(arr[i]);
}
free(arr);

Can you explain why what i am doing is not correct?

The reason why what you are doing is incorrect is that you are presenting a rectangular array to a function that expects a jagged array. The two have very different memory structures.

Rectangular array that you initialize statically is a single, contiguous, block of memory of size 25*sizeof(int). The "second dimension" is added to that flat chunk of memory by compiler's knowledge that the second dimension has exactly five elements. That's how the compiler can apply a "cookie cutter" to the block, partitioning it in five equal blocks of five integers. Accessing an element at (i,j) is translated to adding 5*i+j offset to the base address of arr (a single pointer dereference).

Jagged array, on the other hand, is an array of pointers to arrays. It has a size of 5*sizeof(int*). Each of these pointers can point to a separate array. Accessing an element at (i,j) is translated to accessing arr[i] pointer, and then accessing the value at the offset taken from that value (two pointer dereferences).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • can`t i use statically allocated array? – YAKOVM Nov 24 '13 at 12:42
  • @Yakov You cannot easily allocate a jagged array statically - you would need to allocate each row separately, or use some other magic to assign pointers to pointers statically. You can use "rectangular" arrays which are easy to initialize, but they must have one of their sides fixed. – Sergey Kalinichenko Nov 24 '13 at 12:48
  • @Yakov Here is [a link](http://ideone.com/gRioGo) to an example of making a jagged array statically. Warning: it isn't pretty. – Sergey Kalinichenko Nov 24 '13 at 12:51
  • Converting `int[5][7]` to a `std::array` or equivalent statically is not that hard. And `operator int**()` is also easy. Now, why you would want to do this, I am unsure. – Yakk - Adam Nevraumont Nov 24 '13 at 12:56
  • @dasblinkenlight - can you explain why what i am doing is not correct? – YAKOVM Nov 24 '13 at 12:58
1

I propose the use of a one-dimensional array which holds the two-dimensional data. This makes handling and passing of the array a lot easier and it's not that difficult to understand in my opinion...

void f (int* arr, int n) // n is a size of each dimension

int arr [25] = {0,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,0,1,1,1,0};

in your function you could traverse the data like this:

...
for (int row = 0; row < n; ++row)
{
    for (int col = 0; col < n; ++col)
    {
        arr[row*n + col] = 1; // or something else...
    }
}
xmoex
  • 2,602
  • 22
  • 36