Thread: passing 2D array of ptrs as parameter

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    passing 2D array of ptrs as parameter

    OK, say I have this
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct MyType
    {
    	double x;
    	int y;
    };
    
    int main()
    {
    	MyType t1;
    	MyType t2;
    	MyType t3;
    	MyType t4;
    	MyType* pMT = &t1;
    	MyType* pMT2 = &t2;
    	MyType* pMT3 = &t3;
    	MyType* pMT4 = &t4;
    	MyType* Map[2][2] = {{pMT, pMT2},
    						 {pMT3, pMT4}};
    	return 0;
    }
    How would the prototype for a function, that would take the Map array as an argument, look like? Like for example if I just wanted a function that printed all the values in the array
    Code:
    void PrintArray(??);
    And one quick thing, would it be possible to have a pointer that points to the Map array that I can use to access everything? Or am I going in the totally wrong direction with this whole thing here?
    Thanks in advance,
    -Psycho

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Thanks for the help Salem

    Would there be a way to do the function parameter say if the program didn't know the dimensions of the multi-dimensional array? So that you could maybe pass:
    MyType* Map[2][2]
    or
    MyType* World[3][5]

    this is where I'm stuck, I can't think of any way to do that in my knowledge, because I know when you define a multi-dimensional array you can leave the first [] empty but you have to define the second set.

    Well anywho, thanks in advance,
    -Psycho

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm curious why Salem declared the function prototype with both dimensions, since the leftmost dimension is not part of the type of an array. The type of your array, as you apparently realize, is:

    MyType*[][2]

    But then you seem to forget about the type when you ask if you can pass a 3 x 5 array to the function. What's the type of a 3x5 array? Is it the same type as your function parameter?

    "Would there be a way to do the function parameter say if the program didn't know the dimensions of the multi-dimensional array?"

    Your program can't store data in an array unless it knows the dimensions beforehand. At some point, the program has to set aside the memory, and you have to tell it how much to set aside. You can wait until run time to dynamically declare a two dimensional array depending on some input from the user, but before you can send the array to the function, you have to create it, and at that point your program knows the size of the array. That may be what you are after. In that case, your function parameter would be of type:

    MyType**

    which may look strange, but it just means it's a pointer to a location in memory where there is an array of pointers. To dynamically create a two dimensional array, you need to do this:
    Code:
    MyType** Map= new MyType*[first_dimension];
    for(int i=0; i<first_dimension; i++)
    {
        my_array[i] = new MyType[second_dimension];
    }
    
    ...
    ...
    PrintArray(Map);
    To release the memory you need to do this:
    Code:
    for(int p=0; p<first_dimension; p++)
    {
         delete [] Map[p];
    }
    delete [] Map;
    Last edited by 7stud; 06-13-2003 at 12:03 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Allright thank you both

    As for newbie, I'm only one in the area of pointers. I know how to pass arrays to functions, just not how to do it when the width is undetermined.

    Well I'll try these methods out, and I'll stop bugging you with it
    Thanks again,
    -Psycho

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help passing nxn array to 2d array.
    By beglaryanh in forum C Programming
    Replies: 2
    Last Post: 06-06-2009, 05:23 PM
  2. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM