Thread: multidimensional array as a function argument

  1. #1
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    multidimensional array as a function argument

    hello!
    I am trying to call a function that has multi dimensional array as one of its arguments. My Dev C++ gives me crap with this code:
    Code:
    #include <iostream>
    using namespace std;
    
    void printarray( int arr[] [3] [3],int verlen = 3,int horlen = 3);
    int main(void)
    {
    int x[3][3];
    for (int b(0);b<4;b++){
        for(int c(0); c<4; c++){
                     x[c][b] = c;
                     
                     }           
                     }
    printarray(x,3,3);
    cin.get();
    }
    
    void printarray( int arr [3] [3],int verlen = 3,int horlen = 3){
         for(int x(0);x<=verlen; x++){
            for(int y(0); y<=horlen;y++){
                    cout<< arr[x][y];
                    }
            cout<<"\n";
            }
            }
    Dev C++ has this to say about my code:
    In function `int main()':
    cannot convert `int (*)[3]' to `int (*)[3][3]' for argument `1' to `void printarray(int (*)[3][3], int, int)'
    [main.o] Error 1
    What am i doing wrong?
    We're all Fu##ed aren't we?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Prototype:
    Code:
    void printarray( int arr[] [3] [3],int verlen = 3,int horlen = 3);
    Definition:
    Code:
    void printarray( int arr [3] [3],int verlen = 3,int horlen = 3)
    See a difference?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The first problem is that your printarray function declaration (prototype) and function definition do not match. The next problem is that since your function declaration declares default arguments, your function definition should omit them instead of declaring them again.

    Oh, and your indentation can be improved.

    EDIT:
    Looking more closely at the code, you have another problem: you are accessing the array out of bounds. For example, you access x[3][3] both in main() and in printarray().
    Last edited by laserlight; 09-29-2008 at 12:11 PM.
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Oh, and your indentation can be improved.
    This is Dev-C++'s fault.
    It create this poor, bad indentation by default if you don't intervene.
    I'd say switch IDE.
    A list of free ones: http://cpwiki.sf.net/IDE
    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
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    ok I guess the tutorial i am reading is shady,

    it says that:
    Code:
    void procedure (int myarray[][3][4])
    is a valid function prototype
    but when I try
    Code:
    void printarray( int arr[] [4] [4],int verlen = 3,int horlen = 3);
    int x[4][4];
    int main(void)
    {
    for (int b(0);b<5;b++){
       for(int c(0); c<5; c++){
                                x[c][b] = c;                
                              }                  
                          }
    printarray(x,4,4);
    cin.get();
    }
    
    void printarray( int arr[] [4] [4],int verlen,int horlen){
         for(int x(0);x<=verlen; x++){
            for(int y(0); y<=horlen;y++){
                    cout<< arr[x][y];
                    }
            cout<<"\n";
            }
            }
    it gives me an error but...
    Code:
    void printarray( int arr [4] [4],int verlen = 3,int horlen = 3);
    int x[4][4];
    int main(void)
    {
    for (int b(0);b<5;b++){
       for(int c(0); c<5; c++){
                                x[c][b] = c;                
                              }                  
                          }
    printarray(x,4,4);
    cin.get();
    }
    
    void printarray( int arr [4] [4],int verlen,int horlen){
         for(int x(0);x<=verlen; x++){
            for(int y(0); y<=horlen;y++){
                    cout<< arr[x][y];
                    }
            cout<<"\n";
            }
            }
    is fine...
    We're all Fu##ed aren't we?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course it is. The number of [ and ] tells the compiler how many dimensions the array you're passing is supposed to have.
    And in the first example, you tell the compiler it has 3, while you pass an array that has 2.

    Also,
    int main(void)
    can be written as
    int main()
    (same thing, no need for void.)

    And the variable x should be local rather than global.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In a function prototype or definition, int arr[] [4] [4] is equivalent to int arr[4] [4] [4]. It is very different than int arr [4] [4]. If you want to leave out the size of the first dimension (which is commonly done) then you would use:
    Code:
    void printarray( int arr [] [4],int verlen,int horlen)

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ok I guess the tutorial i am reading is shady
    Or flat out wrong. Post a link to it.

    > is a valid function prototype
    It is, if you were passing a 3D array to it, and not a 2D array.

    Copy/paste always works, and saves a hell of a lot of thinking.
    If you have
    int a[X][Y];

    Then the prototype for a function would be
    void foo ( int a[X][Y] );
    The occasional tool will complain about the first dimension not being empty, but that's easily fixed to become [ ][Y]

    And the call would be
    foo( a );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM