# Thread: Pointers to 2D arrays

1. ## Pointers to 2D arrays

Hi, how do I declare pointers to 2D arrays, and how can I then pass these as arguments to a function? The syntax must be different to that of 1D arrays, since I keep getting compiler errors when doing either of these things. Cheers

2. Code:
`int (*ptr)[10][10];`
Perhaps?

It really depends on what you actually mean by "pointer to 2D arrays".
You may also mean a pointer to pointer to pointer, if you are dynamically allocating the content of the array.

--
Mats

3. Can you post what you are trying to do.

4. I'm trying to write a function that takes two (same-size) 2D arrays of numbers and adds them element wise, returning a third 2D array.

using:

Code:
```int add(int (*a1)[][],int (*a2)[][])
{...}```
produced a compiler error, "array type has incompatible element type"

5. Code:
`int add(int (*a1)[][10], int (*a2)[][10], int (*a3)[][10])`
The outer dimension must be specified. Also, you would need an argument for the 3rd array to store the result (you cannot return arrays; only pointers, but that would mean you have to use malloc).

6. You MUST specify the all dimensions except the one to the most left when passing arrays into a function. If you don't, the compiler will not be able to calculate the positions of the elements in the array.

Edit: And most likely, what you actually want to do is to NOT pass a pointer.

--
Mats

7. The innermost subscript is the only one that can be left out. All the others must be specified as in.
Code:
`int add(int (*a1)[][10],int (*a2)[][20])`
and your return type is an int or a pointer to a 2D array??

8. Originally Posted by Elysia
Note that that is a 3D array.
Correct the array type is 3D but a1 (or a2 for that matter) is a pointer to a 2D array. The OP must provide input or show how the arrays have been defined.

9. Ah, you are right... >_<
That just goes to show why pointers to arrays tend to be confusing... Grrr.
It's also possible to just "declare" them as 2D arrays...
Code:
`int add(int a1[][10], int a2[][10], int a3[][10])`
Less confusion, no?

11. Originally Posted by itCbitC
The innermost subscript is the only one that can be left out. All the others must be specified as in.
Code:
`int add(int (*a1)[][10],int (*a2)[][20])`
and your return type is an int or a pointer to a 2D array??
I tried this:
Code:
```#include <stdio.h>

int add(int (*a1)[][3], int (*a2)[][3], int (*a3)[][3]){
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
a3[i][j]=a1[i][j]+a2[i][j];
}
}
return 0;
}

int main (){

int size1,size2;
int i,j;

printf("enter the size of a square matrix:");
scanf("%d",&size1);

int a1[size1][size1];

for(i=0;i<size1;i++){
for(j=0;j<size1;j++){
printf("enter element %d,%d: ",i+1,j+1);
scanf("%d",&a1[i][j]);
}
}

printf("enter elements of second matrix:\n");

int a2[size1][size1];

for(i=0;i<size1;i++){
for(j=0;j<size1;j++){
printf("enter element %d,%d: ",i+1,j+1);
scanf("%d",&a2[i][j]);
}
}

int a3[size1][size1];

int *ptr1=&a1;
int *ptr2=&a2;
int *ptr3=&a3;

printf("the resultant matrix is:\n\n");

for(i=0;i<size1;i++){
for(j=0;j<size1;j++){
printf("%d ",a3[i][j]);
}
printf("\n");
}

return 0;
}```
which gave me output:

Code:
```addm.c: In function ‘add’:
addm.c:7: error: invalid use of array with unspecified bounds
addm.c:7: error: invalid use of array with unspecified bounds
addm.c:7: error: invalid use of array with unspecified bounds
addm.c:7: error: invalid operands to binary +
addm.c:43: warning: initialisation from incompatible pointer type
addm.c:44: warning: initialisation from incompatible pointer type
addm.c:45: warning: initialisation from incompatible pointer type```
Preferably I want to be able to pass pointers to two arbitrarily but same sized arrays to the function, have it create a third array, and return a pointer to this array. How can I do that?

Thanks

P.S. I'm glad I'm not the only one who's confused ^^

12. Firstly,
Code:
```	int *ptr1=&a1;
int *ptr2=&a2;
int *ptr3=&a3;

This should just be:
Code:
`	addm(a1, a2, a3);`
And you want to change your function to...
Code:
`int add(int a1[][3], int a2[][3], int a3[][3]){`
The problem stems from the fact that by taking the address of a 2D array, you do not get a raw pointer (ie T*), but rather T (*)[x][y].

And for the first errors... well, array size must be constant, so you'll have to look into malloc and free.

13. Thanks, I was under the impression I could only pass arrays to functions as pointers. Well if not, great, since it's confusing as hell.

As for the first errors, I thought I had already specified the array size to be constant, with e.g.

Code:
` int a1[][3]`
However, this was just a test and what I really want is to be able to pass an arbitrary sized array. I'll have a look into malloc and free, but can you explain why the above is not a constant sized array?

14. First of all is the function named addm() or add()? and
Why go through so much trouble when you can simply use the array name itself as a pointer, as in.
Code:
```/* all these simply add to the complexity */
int *ptr1=&a1;
int *ptr2=&a2;
int *ptr3=&a3;
```addm(a1, a2, a3);