# Thread: double astrix 2D array signature question..

1. ## double astrix 2D array signature question..

i have a 2D integer array called arr.
which is passed into a function as
what1(arr,6,5)

but the signature is of the function is
int what1(int **arr, int m, int n)

so maybe there is a mistake and
it should be what1(&arr,6,5) instead of what1(arr,6,5)
??

2. can i do arr[0][0] = 0; for arr
if arr is passed instead of &arr
will it save the change after the function ends??

3. Actually, both are wrong. A 2D array can not by simply passing it to a function, be turned into a pointer to pointer.

If you post the whole code, it may be possible to tell you how to make it correct, but a pointer to pointer should contain an address of a pointer. Which is not the address of the first element in an 2D array.

--
Mats

4. its a question from a test:
i was asks what what1(arr,6,5) expression will return
??
arr is a 2D array which looks like this:
0 1 1 0 1
0 0 1 0 0
0 0 1 1 1
0 1 0 0 0
1 0 0 1 1
1 0 1 1 0

Code:
```int what1(int **arr, int m, int n){
int i, j, tmp, stam=0;

for(i=0; i<m; i++)
for(j=0; j<n; j++){
tmp = what2(arr,i,j,m,n);
if (tmp>stam)
stam = tmp;
}
return stam;
}

int what2 (int **arr, int row, int col, int m, int n){
int i,j,tmp, stam=0;

if (row < 0 || row >= m || col < 0 || col >= n) return 0;
if (arr[row][col] == 0) return 0;

arr[row][col] = 0;
for(i=-1; i<2; i++)
for(j=-1; j<2; j++){
if(!i && !j) continue;
tmp = 1 + what2(arr, row+i, col+j, m, n);
if (tmp > stam)  stam = tmp;
}
arr[row][col] = 1;

return stam;
}```

5. Right, so the posted code doesn't actually say that it is a statically allocated (or statically sized) 2D array, simply that it IS a 2D array. It would work if the array is allocated dynamically. [Or the code produces the correct pointers to form an array of pointers to each row of the 2D array].

--
Mats

6. be sure that the code doesnt have errors.

whats dynamically allocated?

is it using malloc to create the array??

and staticaly allocated is saying int arr[5][6];

correct?

7. Dynamically allocated -> using arr = malloc(...); arr[n] = malloc(...) rather than something like arr[5][6].

--
Mats

8. ok so lets assume that its dinamically allocated

why it passes the arr as

can i do arr[0][0] = 0; for arr
if arr is passed instead of &arr
will it save the change after the function ends??

what2
has that line
Code:
`arr[row][col] = 0;`
and i cant decide what happens at the beginning when arr[0][0] = 0;
because arr is passed not &arr
so the change which that line will make
will not exist after what2 ends
??

9. Because arr is (presumably) declared as int **arr? That would make sense, don't you think?

--
Mats

10. so because arr is passed and not &arr
so i cant change arr itself
only *arr stuff
arr[0][0] is like saying *((*p+0)+0)

so i can change every cell in the array

so i can change the signature of the function to
what1(int *arr, int m, int n)
and it will work the same way
correct?

11. does *(&arr) equals arr[0][0]
??

i think its not true

arr[0][0]=*(*arr))

12. Originally Posted by transgalactic2
so because arr is passed and not &arr
so i cant change arr itself
only *arr stuff
arr[0][0] is like saying *((*p+0)+0)

so i can change every cell in the array
arr[0][0] is *(*(p+0)+0), but other than that typo your understanding of what you can and can't change is correct here.

In addition to changing arr[0][0], you can also change arr[0]. If you wanted to add extra columns to each row, for example, you could call realloc() on all of the arr[i]'s and the change would remain after you return from the function.

But you can't add rows. That would require you to change arr itself, and any change to it will be lost when the function returns as you mentioned.

so i can change the signature of the function to
what1(int *arr, int m, int n)
and it will work the same way
correct?
If you only wanted to work on one row inside what1, I guess you could pass in a particular arr[row], but you'd lose the ability to do arr[row][col] inside that function. Since arr is a pointer to int, arr[row][col] wouldn't make sense since it requires two dereferences to get to a value. That doesn't work since is only a pointer, not a pointer to a pointer. You'd instead have something like

what1(arr[row], num_cols, which_col);

in main() and inside what1() you'd do

arr[col] = 0;

13. if its a 2D array then the signature should be int**

and we pass it as &arr

but when we pass it as arr
it makes no sense

??

14. Originally Posted by transgalactic2
and we pass it as &arr

but when we pass it as arr
it makes no sense
What is the type of arr, and what is the type of &arr? Which type matches int**?

15. i remember in previous examples in this forum
that inorder to use int** we need to pass &arr
so we could make changes on *arr