2D array pointer

This is a discussion on 2D array pointer within the C Programming forums, part of the General Programming Boards category; I got the following: Code: #include <stdio.h> void print(int *array); int main(void) { int array[5][5] = {{1,2,3,4,5}, {1,2,3,4,5}}; print(&array[0][0]); } ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    2D array pointer

    I got the following:
    Code:
    #include <stdio.h>
    void print(int *array);
    int main(void)
    {
    	int array[5][5] = {{1,2,3,4,5}, {1,2,3,4,5}};
    	print(&array[0][0]);		
    }
    
    void print(int *array)
    {
    	int i, j;
    	for(i=0; i<=4; i++)
    	{
    		for(j=0; j<=4; j++)
    		{
    			printf("%d\n",array[i][j]);
    		}
    	}
    }
    Whats wrong with it? i get the error:

    Code:
    stringArray.c: In function `print':
    stringArray.c:16: error: subscripted value is neither array nor pointer

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is because you are passing an array as a pointer to the first element of the string.

    Now, the C language will translate arrays to pointers in itself, but the way that indexing of multidimensional arrays work, you will have to provide the dimensions of all but the first dimension.

    --
    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
    Aug 2007
    Posts
    270
    so like this:
    print(&array[][0]);

    But that doesnt work?
    Last edited by taurus; 10-29-2008 at 10:38 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    so like this:
    print(&array[][0]);
    Erm, no, that will not compile. What I'm saying is that if you want to use a 2D array in a function, that function needs to know the size of all dimensions except the first one, so that it can multiply the sizes of the different dimensions to form the linear offset in a 1D fashion (there is no such thing as 2D or 3D memory in computers, so ALL array indices need to be made into a linear 1D reference).

    In this case, you need to declare your print function with:
    Code:
    void print(int array[][5]);
    or
    Code:
    void print(int array[5][5]);
    The call should be made as:
    Code:
       print(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.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    But i want to play around with pointers so how would that work the same way i used int *array?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That gets tricky. You need to pass the size in X and Y dimension. Something like this:
    Code:
    print(int *arr, int xsize, int ysize);
    And then calculate the actua y, x coordinates from that.

    --
    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
    Aug 2007
    Posts
    270
    How do i calculate the offset?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    How do i calculate the offset?
    Think a bit about that one...

    --
    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.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    ah ok i found a way:
    *array
    then increment that

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    ah ok i found a way:
    *array
    then increment that
    Erm, well, that's not going to allow you a random access to the array, but sure. What if you have a 10000 x 10000 x 10000 array, and you need to find location [500][3800][8716]? You don't really want to traverse the array one element at a time to find it, do you?

    --
    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.

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    yea thats true.
    then you could multiply those 3 numbers (addition) and say *(array + addition)
    and that would work yeah? is that what you were thinking?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by taurus View Post
    yea thats true.
    then you could multiply those 3 numbers (addition) and say *(array + addition)
    and that would work yeah? is that what you were thinking?
    No.
    Remembering that the rightmost number changes fastest, and to keep things small using a 5x5 array, how would things be laid out in memory? Where is array element [4][3]? Array element [2][3]? Can you spot a pattern?

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    so element[4][3] would be at row*4 + 3?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right.

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Ok. Check this code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void print(int** array)
    {
    	int i, j;
    	for(i=0; i<4; i++) {
    		for(j=0; j<4; j++)
    			printf("%d\t",array[i][j]);
    		printf("\n");
    	}
    }
    
    int main()
    {
    	int i,j;
    	int array[4][4];
    	for(i=0; i<4; i++)
    		for(j=0; j<4; j++)
    			array[i][j] = 123;
    	int** ptr_array;
    	int* outer_ptr_array[4];
    	for (i = 0; i < 4; ++i)
    		outer_ptr_array[i] = array[i];
    	ptr_array = outer_ptr_array;
    	print(ptr_array);
    }
    So you see that you can assign a pointer to a 2D array indirectly as above. That is in case you have a function that wants int** and you don't want to change it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 07:14 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21