pass a multi-dimensional array by argument

This is a discussion on pass a multi-dimensional array by argument within the C++ Programming forums, part of the General Programming Boards category; hello what's the correct syntax to pass a multidimensional array as argument to a function? I've tried many times but ...

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    pass a multi-dimensional array by argument

    hello
    what's the correct syntax to pass a multidimensional array as argument to a function?
    I've tried many times but I always get some error.

    a brief example:
    Code:
    void manage_table (int ???) {
    	table[x][y] = z;
    }
    
    int main () {
    
    	int table[10][10];
    	manage_table (table);
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Pass it the same way you declare it.
    Code:
    void manage_table (int table[10][10]) {
    	table[x][y] = z;
    }
    
    int main () {
    	int table[10][10];
    	manage_table (table);
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void manage_table (int table[][10]);
    You always need to specify ALL dimensions except the first one in C.

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

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    hmm my example doesn't tell exactly what I really need.
    I need to declare array's dimensions at runtime, may that be a trouble?

    Hope this example will explain better:
    Code:
    void manage_table (???, int x, int y) {
    	
    	table[x][y] = <expression>;
    }
    
    int main () {
    
    	int width = 12;
    	int height = 8;
    
    	int table[width][height];
    	manage_table (table, x, y);
    }
    Last edited by carlorfeo; 03-03-2008 at 09:44 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what did you not understand in the explanation given - the fact that you have two more arguments doesn't influence how you specify your 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.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I need to declare array's dimensions at runtime, may that be a trouble?
    It doesn't work like that. You can't set the size of an array at runtime unless you manage the array manually with pointers or use a vector object instead of an array.

  7. #7
    coder
    Join Date
    Feb 2008
    Posts
    127
    yes, the two more arguments don't matter, I've added them to make the code more clear.

    Quote Originally Posted by matsp
    You always need to specify ALL dimensions except the first one in C.
    The real difference is that I cannot specify the second dimension, since it is defined at runtime.
    I know that one fast solution would be using a mono-dimensional array,
    Code:
    int table[width * height]
    ...but so would be more complicated to program.
    Is there another possible solution?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as it's C++, why not use a vector of vector and simply let the array grow as it needs to?

    Otherwise, implement your own 2D array class, and hide all the complexity inside it. If it's always a square you could also enforce that in the class.

    --
    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
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by Banana Man
    It doesn't work like that. You can't set the size of an array at runtime unless you manage the array manually with pointers or use a vector object instead of an array.
    Ok, probably using vectors will be the best solution for me.
    Thank you both

  10. #10
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by matsp
    Seeing as it's C++, why not use a vector of vector and simply let the array grow as it needs to?
    I've made a mistake in my examples: the array declaration inside the main function.
    My program creates that temporary array inside a function, so it doesn't need to be resized: I don't need it anymore after the function returns.
    The trouble is that I need to pass the array to another recursive function.
    Finally I'll choose for a vector of vectors, you are right.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why not simply use dynamic memory allocation ?

    Code:
    int a, b;
    std::cin >> a;
    std::cin >>&#160;b;
    
    int** myptr = new int*[a];
    for(int i = 0; i < a; i++)
       myptr[i] = new int[b];

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Why not simply use dynamic memory allocation ?
    Due to the good old question: How do I deal with memory leaks?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Why not simply use dynamic memory allocation ?
    I have yet to hear a single reason why simple dynamic memory allocation should be used instead of a vector in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 04:27 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Replies: 3
    Last Post: 04-02-2002, 12:39 PM
  4. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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