Thread: Passing Multidementional arrays

  1. #1
    Registered User
    Join Date
    Sep 2008

    Passing Multidementional arrays

    I have two multidementional I wish to, byref (as all arrays in c++ are passed), pass as a parameter to a function. When I attempt to do this, the compiler throws a bunch of errors. How do you do this properly?

    #include <iostream>
    using namespace std;
    void displayArraysContents(char*[][], char*[][], const int size)
    int main ()
         char* testArray1[9][9];
         char* testArray2[9][9];
         for (int i = 0; i < 9; i++)
              for (int i2 = 0; i2 < 9; i2++)
                   testArray1[i][i2] = "XX";
                   testArray2[i][i2] = "YY";
         return 0;
    void displayArraysContents(char* array1[][], char* array2[][], const int size)
            // Code to cout array contents (I have this and it works).

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    Include the size of the outer dimension:
    void displayArraysContents(char* array1[][9], char* array2[][9], const int size)

    We had a recent discussion on why you must do that, but basically it is because the compiler uses a formula to calculate the offset from the start of the array to read/write to/from and it needs to know the size of the outer dimension, since it is one of the variables in the formula.

    Also, you should beware that string literals should be const:
    And even better yet, make an array of std::string instead of char*.
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    All array sizes (except perhaps the first) must be included in the function prototype/parameter list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidementional arrays, need concept explanation??
    By Leojeen in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 11:56 AM
  2. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM