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
Printable View
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
Perhaps?Code:int (*ptr)[10][10];
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
Can you post what you are trying to do.
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:
produced a compiler error, "array type has incompatible element type"Code:int add(int (*a1)[][],int (*a2)[][])
{...}
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).Code:int add(int (*a1)[][10], int (*a2)[][10], int (*a3)[][10])
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
The innermost subscript is the only one that can be left out. All the others must be specified as in.
and your return type is an int or a pointer to a 2D array??Code:int add(int (*a1)[][10],int (*a2)[][20])
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...
Less confusion, no?Code:int add(int a1[][10], int a2[][10], int a3[][10])
No question about that! :cool:
I tried this:
which gave me output: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;
addm(ptr1,ptr2,ptr3);
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;
}
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?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: In function ‘main’:
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
Thanks
P.S. I'm glad I'm not the only one who's confused ^^ :)
Firstly,
This should just be:Code:int *ptr1=&a1;
int *ptr2=&a2;
int *ptr3=&a3;
addm(ptr1,ptr2,ptr3);
And you want to change your function to...Code:addm(a1, a2, a3);
...to avoid headaches.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.
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.
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?Code:int a1[][3]
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.
You can reduce all the above to:Code:/* all these simply add to the complexity */
int *ptr1=&a1;
int *ptr2=&a2;
int *ptr3=&a3;
addm(ptr1,ptr2,ptr3);
Code:addm(a1, a2, a3);
/* so the receiving parms in addm() become */
addm(int (*a1)[size1], int (*a2)[size1], int (*a3)size3])
itCbitC, this does not work since it says pointers to the arrays, hence the need to use & in that case.
It also causes trouble inside the function, as it's not mere arrays anymore, but pointers to arrays, so to avoid confusion, I'd go by my example.
bertazoid: It's not the passing of arrays that is the trouble, but the actual creation of them in your main function. The errors clearly point to those lines.