Arrays as arguements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    Arrays as arguements

    Hello, I am having touble passing multidimensiona l arryas as an arguement to a function. This is what I have tried:
    Code:
    void printarray(char array[5][5]){
        for (int x = 0; x < 5; x++){
            for (int y = 0; y < 5; y++){
                cout << array[x][y];}
            cout << endl;}}
    
    int main(){
        char array[5][5];
        printarray(array);
        // rest of program
    I've also tried declaring the function like: void printarray(char array[][5])
    void printarray(char *array[5][5])
    void printarray(char *array[][5])

    and calling it like: printarray(&arr ay)
    printarray(*arr ay)

    and none of those work.

    The error message is:
    Code:
     cannot convert `char (*)[4]' to `char*' for argument `1' to `void printarray(char*)'
    Does anybody know what I'm doing wrong?
    I'm using Dev C++ on Windows XP.

    Thanks in advance.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Never mind, sorry guys.
    I got it all figuered out.
    Don't need help anymore.
    Thanks

    Comment

    • rgb
      New Member
      • Aug 2006
      • 37

      #3
      pass a pointer of the array as an argument.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by rgb
        pass a pointer of the array as an argument.
        That's not needed: arrays are passed as a pointer to their first element in every
        calue constant. The first element of a two dimensional is a one dimensional
        array so a pointer to it looks like this:
        Code:
        void print(char (*row)[5]) { ... }
        Of course for the print function itself there's no way to determine how many
        rows the two dimensional matrix has.

        kind regards,

        Jos

        Comment

        Working...