Thread: returning a 2D array from a function

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    returning a 2D array from a function

    Hi all. I am very rusty in C so please bear with me. I want to send a function an array and then return a different array. I understand how to send the array to a function, but not to get it back. Is this the best way of passing arrays from functions by initializing the arrays in the main function?

    My error that I ended up is this:

    error: expression must be a pointer to a
    complete object type
    another_array[i][j]=cnt++;
    What is wrong with this????

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void function(int array[], int another_array[][]);
    
    int main(void)
    {
    	int i;
    	int array[10];
    	int another_array[5][5];
    	
    	//Create test array
    	for(i=0;i<10;i++)
    	{
    		array[i]=i;
    	}
    	function(array,another_array);
    	
    	for(i=0;i<5;i++)
    	{
    		printf("array[%d]=%d\n",i,array[i]);
    		printf("another_array[%d]=%d\n",i,another_array[i][1]);
    	}
    	
    	return 0;
    }
    
    
    void function(int array[], int another_array[][])
    {
    	int i,j,cnt=0;
    	
    	for(i=0;i<5;i++)
    	{
    		for(j=0;j<5;j++)
    		{
    			another_array[i][j]=cnt++;
    		}
    	}
    	
    }
    Thank you for your time ^^

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add a 5 to the second dimension of the parameter:
    Code:
    another_array[][5];
    Now re-compile and run the program.

    Now put the 5 into the first dimension of the parameter, and re-compile, leaving the second dimension just empty brackets: [].

    And re-compile.

    What do you surmise from this?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    IT WORKS. Reason: I have to define the length of its columns? Why only the columns? And why is this not the same for a 1-D array?

    Thanks >_< Trying to understand...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sauwen
    IT WORKS. Reason: I have to define the length of its columns? Why only the columns? And why is this not the same for a 1-D array?
    Because when an array is passed as an argument, it is converted to a pointer to its first element. So if you pass an array of 5 ints, it is converted to a pointer to an int, but if you pass an array of 10 arrays of 5 ints, it is converted to a pointer to an array of 5 ints.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    I still don't fully grasp this concept... Please bear with me..

    So if you pass an array of 5 ints, it is converted to a pointer to an int, but if you pass an array of 10 arrays of 5 ints, it is converted to a pointer to an array of 5 ints.
    Passing an array of 10 arrays with 5 ints? You are referring to a 2D array that is a 10x5, right? So you need to put the 5 in the another_array[][5] so that the pointer is going to an int instead of an array since you cannot send arrays?

    So I assume that for a 3D array it is the same thing where as if you initialize another_array[5][5][5], you need to send and return like this:

    void function(int array[], int another_array[][5][5]);

    Thank you! ^^

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The second bracket number is the offset the program uses, to find the next row (which is itself, a pointer to it's row):

    So the jump from row to row is just:
    Code:
    for(i=0;i<MAXROWS; i++) {
      for(column=0;j<MAXCOLS; columns++) {
    
         array[ i * offset ][columns];
    
      }
    }
    0, 5, 10, 15, 20, etc. All right where the program needs to jump to, to find the next ponter of the sub array for that row.

    Without that offset (five in this case), how would the program know where to go to, in an array, that was in a called function, other than the one that created it?

    IT WORKS -- you need not sound so surprised!

    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. returning a 2D array in a function
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-05-2002, 08:56 AM