-
February 4th, 2005, 05:22 PM
#1
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!
-
February 4th, 2005, 05:25 PM
#2
Re: multidimension array as function argument
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
-
February 4th, 2005, 05:31 PM
#3
Re: multidimension array as function argument
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.
-
February 4th, 2005, 05:33 PM
#4
Re: multidimension array as function argument
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|