Thread: Pointers to 2D arrays

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    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
    PHI is one 'H' of alot more interesting than PI!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post what you are trying to do.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    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"
    PHI is one 'H' of alot more interesting than PI!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Last edited by Elysia; 02-26-2009 at 03:29 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Elysia View Post
    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. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    No question about that!

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by itCbitC View Post
    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;
    
    	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;
    }
    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: 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
    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 ^^
    Last edited by bertazoid; 02-26-2009 at 03:58 PM.
    PHI is one 'H' of alot more interesting than PI!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Firstly,
    Code:
    	int *ptr1=&a1;
    	int *ptr2=&a2;
    	int *ptr3=&a3;
    
    	addm(ptr1,ptr2,ptr3);
    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]){
    ...to avoid headaches.
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    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?
    PHI is one 'H' of alot more interesting than PI!

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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(ptr1,ptr2,ptr3);
    You can reduce all the above to:
    Code:
    addm(a1, a2, a3);
    /* so the receiving parms in addm() become */
    addm(int (*a1)[size1], int (*a2)[size1], int (*a3)size3])

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  2. arrays of pointers to function?????
    By andyzjk in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2005, 09:55 AM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM