#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0

    C++ Arrays as Parameters to Functions


    I've no problem creating a user-defined function with a one dimensional array.

    Example:

    Code:
    void Seat_Taken(char airplane1[], int Row_Choice1); //Function prototype
    
    int main()
    {
    .
    .
    Seat_Taken(airplane, row_choice)
    . 
    .
    }
    void Seat_Taken(char airplane1[], int Row_Choice1) //Function heading
    {
    airplane1[Row_Choice1-1]='x';
    }
    Works perfectly fine, however, when I try creating a multidimensional 2D ray:
    Code:
    void Seat_Taken(char airplane1[][], int Row_Choice1, char Col_Choice1);
    int main()
    {
    .
    .
    Seat_Taken(airplane, Row_Choice, Col_Choice)
    .
    .
    }
    void Seat_Taken(char airplane1[][], int Row_Choice1, char Col_Choice1)
    {
       airplane1[Row_Choice1-1][Col_Choice1-65]='x';
    }
    It gives me this error x 2:
    error C2087: '<Unknown>' : missing subscript

    and it's pointing to the function prototype, and function heading. I figured that i'm leaving out something because I have a 2D ray instead of a one dimensional ray - but I really don't know what i'm leaving out.

    Thanks in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    UK
    Posts
    41
    Rep Power
    12
    I believe you have to supply the number of elements in the second dimension, eg.
    Code:
    functionX(char array[][3],  int & num)
    {
       ...
    }
    See if it compiles with that.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0

    Thanks


    Works perfect now :)
  6. #4
  7. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    No offence, but any beginner's book would have given you the answer ;-)

IMN logo majestic logo threadwatch logo seochat tools logo