CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2005
    Posts
    58

    multidimension array as function argument

    If I want to define a function which has a argument is a multidimension array, such as:

    int f(data[][], int N)
    {
    ...
    }

    void main()
    {
    int p[3][3]={...};
    int a=f(a,3)
    }

    When compiled, there is a error, but if I define f as
    int f(data[][3], int N)
    {
    ...
    }

    that's ok. If there are any other mothod to solve the problem, since the second dimension of the array may not be decided. Thanks!

  2. #2
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: multidimension array as function argument

    Quote Originally Posted by greghua
    If I want to define a function which has a argument is a multidimension array, such as:

    int f(data[][], int N)
    {
    ...
    }

    void main()
    {
    int p[3][3]={...};
    int a=f(a,3)
    }

    When compiled, there is a error, but if I define f as
    int f(data[][3], int N)
    {
    ...
    }

    that's ok. If there are any other mothod to solve the problem, since the second dimension of the array may not be decided. Thanks!
    Yes.
    You can use a vector<vector<int> > type, which is a safer way to go.

    You can also use a type**
    int ** data

    Check out the following link for other methods of creating a 2D array:
    http://www.tek-tips.com/faqs.cfm?fid=5575
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: multidimension array as function argument

    Quote Originally Posted by greghua
    that's ok. If there are any other mothod to solve the problem, since the second dimension of the array may not be decided. Thanks!
    When you say it may not be decided, do you mean it may not be decided at compile time?
    If the size is determine at compile time, then another option you can use is templates.

    Code:
     template<class T> 
    int f(T &data, int N)
    {
    }
    The above function will work for different concrete 2D types.

    If you use this method, you also will not need the second argument to determine the size.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  4. #4
    Join Date
    Nov 2004
    Location
    Virginia, The lovers' state
    Posts
    64

    Re: multidimension array as function argument

    Quote Originally Posted by greghua
    If I want to define a function which has a argument is a multidimension array, such as:

    int f(data[][], int N)
    {
    ...
    }
    Did you know that the main function in C/C++ uses an argument which is in effect a multidimensional array ? check the argv parameter to main, it is typically declared as

    char *argv[]

    char **argv

    You could use that same approach....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured