Passing 2D arrays between functions

This is a discussion on Passing 2D arrays between functions within the C Programming forums, part of the General Programming Boards category; Im a bit confused how I would do the following: I have the following functions: Code: void bkSub(char arrayIn[]) void ...

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

    Question Passing 2D arrays between functions

    Im a bit confused how I would do the following:
    I have the following functions:
    Code:
    void bkSub(char arrayIn[])
    void blackNWhite()
    void cC()
    Now this is what the flow I am looking for:

    bkSub()
    This function after receiving the arrayIn and processing it should pass a new 2D array (float array[height][width]) it created down to blackNWhite()
    blackNWhite()
    This function now receives that 2D array (float array[height][width]), processes it and passes it down to cC()
    cC()
    This now receive the 2D array as above and processes it and then passes back to main a structure info.

    I am not sure how to do the passing of the 2D array between the functions.
    Could someone help?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Pass it with pointers.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Yea I know that but not sure how with 2D arrays

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    void blackNWhite(float array[height][width])
    Rise and repeat. Know that you must specify the outer dimension for it to compile.
    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.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Elysia View Post
    void blackNWhite(float array[height][width])
    Rise and repeat. Know that you must specify the outer dimension for it to compile.
    Yuck.

    Code:
    void blackNWhite(float * array, const size_t rows, const size_t columns)
    Yum.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    I tried this basic one:
    Code:
    void func(char array[])
    {
    	char name[255];
    	strncpy(name, array, 255);
    	printf("%s\n", name);
    	int twoD[5][5];
    	int i, j;
    	for(i=0; i<5; i++)
    	{
    		for(j=0; j<5; j++)
    		{
    			twoD[i][j] = i;
    		}
    	}
    	func2(twoD);
    }
    
    void func2(int array[][5])
    {
    	int i, j;
    	for(i=0; i<5; i++)
    	{
    		for(j=0; j<5; j++)
    		{
    			printf("%d\n", array[i][j]);
    		}
    	}
    }
    But i get the error:
    Code:
    func.h:28: warning: conflicting types for ‘func2’
    func.h:25: warning: previous implicit declaration of ‘func2’ was here
    Actually works now, but how could I do it by passing via pointers?
    Last edited by taurus; 09-27-2009 at 07:06 AM. Reason: works for some reason

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by zacs7 View Post
    Code:
    void blackNWhite(float * array, const size_t rows, const size_t columns)
    Yuck. 2D arrays as 1D array.

    Code:
    void blackNWhite(float array[height][width])
    2D array as 2D array. Yum.

    As for the question, cast the array to float* when you pass it. Pass along the rows and columns to the function, too.
    And as for the warning, you forgot to make a prototype for the function.
    Last edited by Elysia; 09-27-2009 at 08:01 AM.
    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.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Implicit declaration == "I forgot to write a prototype for that function."

    If you want to write a function that can handle arbitrarily-sized arrays, you would need to pass in the memory address of the array itself, and its sizes.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    what about this:
    Code:
    ....
    int twoD[height][width];
    //DO SOMETHING
    func2(twoD, height, width);
    ....
    
    void func2(int array[][], height, width)
    {
    	int i, j;
    	for(i=0; i<height; i++)
    	{
    		for(j=0; j<width; j++)
    		{
    			printf("%d\n", array[i][j]);
    		}
    	}
    }
    How can i pass that array? Whats the best way to do that?
    Last edited by taurus; 09-28-2009 at 02:11 AM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want your func2 to be able to handle any-sized array, the first parameter need to be an int *, and inside you need to handle the arithmetic yourself.

    We just did this fairly thoroughly Friday last week, I think; so, as heretical as it may seem, you may need to read someone else's posts to find more information.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    When you pass a 2D-array, you must specify the size of the outer-most dimensions. I showed with an example before. You had to fill in the width in the array. The height would be optional, but the width is required.
    Or pass an int* along with height & size. It will be treated as a 1D array, however.
    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. Passing Arrays to Functions...
    By txcs in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 10:36 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  3. Passing arrays to functions?
    By gibbofresco in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2006, 11:10 AM
  4. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  5. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM

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