I have a question:
How to pass 2-dimension(or 3-dimension....) array to function?
Using pointer,array parameter?
Could someone teach me?
Thanks!!
Printable View
I have a question:
How to pass 2-dimension(or 3-dimension....) array to function?
Using pointer,array parameter?
Could someone teach me?
Thanks!!
This is one way but only works for fixed-size array.
Code:void init(int array[10][10])
{
int count=0;
for(int i=0;i<10;++i)
for(int j=0; j<10;++j)
array[i][j] = count++;
}
int main(void)
{
int num[10][10];
init(num);
for(int i=0;i<10;++i)
{
for(int j=0;j<10;++j)
{
printf("%d ", num[i][j]);
}
printf("\n");
}
}
You can do like this too. Only the last size must be fixed for 2D array.
void init(int array[][10]);
This is the code to use 3 dimensional array of any size. It is almost the same way for any dimensional array bigger than 3D, except the formula for accessing the array inside the function is different.
Code:#include <stdio.h>
#include <stdlib.h>
void TempFunction(int* m_iArray, int maxx, int maxy, int maxz);
enum
{
MAXX = 7,
MAXY = 8,
MAXZ = 9
};
int main()
{
int ct1,ct2,ct3;
int iArray[MAXX][MAXY][MAXZ];
TempFunction (&iArray[0][0][0], MAXX, MAXY, MAXZ);
printf("ct1\tct2\tct3\tArray\n");
for(ct1=0;ct1<MAXX;ct1++)
{
for(ct2=0;ct2<MAXY;ct2++)
{
for(ct3=0;ct3<MAXZ;ct3++)
{
printf("%d\t%d\t%d\t%d\n",ct1,ct2,ct3,iArray[ct1][ct2][ct3]);
}
}
}
system("pause");
return 0;
}
void TempFunction(int* iArray, int maxx, int maxy, int maxz)
{
int x,y,z;
for(x=0;x<maxx;x++)
{
for(y=0;y<maxy;y++)
{
for(z=0;z<maxz;z++)
{
iArray[((x*maxy)+y)*maxz+z]=2;
}
}
}
iArray[((3*maxy)+6)*maxz+3]=233;
}
Why the last size must be fixed???Quote:
Originally posted by CBasicNet
You can do like this too. Only the last size must be fixed for 2D array.
void init(int array[][10]);
:confused:
Coud I take a pointer type as parameter to pointer an array?
:confused:
Else it wouldn't compile.
I know it.Quote:
Originally posted by CBasicNet
Else it wouldn't compile.
But I would like to know why compiler cann't compile it.
Although the array is declared as multi-dimension, the memory it occupies is actually one-dimensional. The example below shows that.Quote:
Originally posted by Cooker
I know it.
But I would like to know why compiler cann't compile it.
For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next column begins.
Code:void init(int array[][10])
{
int count=0;
for(int i=0;i<10;++i)
for(int j=0; j<10;++j)
array[i][j] = count++;
}
int main(void)
{
int num[10][10];
init(num);
int *pointer=&num[0][0];
for(int i=0;i<10;++i)
{
for(int j=0;j<10;++j)
{
printf("%d ", *pointer);
++pointer;
}
printf("\n");
}
return 0;
}
"For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next column begins."Quote:
Originally posted by Kheun
Although the array is declared as multi-dimension, the memory it occupies is actually one-dimensional. The example below shows that.
For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next column begins.
Code:void init(int array[][10])
{
int count=0;
for(int i=0;i<10;++i)
for(int j=0; j<10;++j)
array[i][j] = count++;
}
int main(void)
{
int num[10][10];
init(num);
int *pointer=&num[0][0];
for(int i=0;i<10;++i)
{
for(int j=0;j<10;++j)
{
printf("%d ", *pointer);
++pointer;
}
printf("\n");
}
return 0;
}
Could u describe that clearly?
I don't know what you talked.
THX.:D
Sorry I made a mistake in my explanation. It should be
For simplicity, let us only look at 2D array. My example illustrates that multi-dimension array is actually constructed from a single dimension array. When we are using indexing in an array like array[i][j], the actual representation in 1D array is *(array + i*width + j). In this case, the width is 10.Quote:
For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next row begins.
As the array is created in main() and passed as a parameter to the init(), the compiler need information to tell init() that the received parameter is a 2D array and has a width of 10. In this case, the program can reference to the correct element within the array when you use array[i][j].
Hope this helps. :)
Oh!Quote:
Originally posted by Kheun
Sorry I made a mistake in my explanation. It should be
For simplicity, let us only look at 2D array. My example illustrates that multi-dimension array is actually constructed from a single dimension array. When we are using indexing in an array like array[i][j], the actual representation in 1D array is *(array + i*width + j). In this case, the width is 10.
As the array is created in main() and passed as a parameter to the init(), the compiler need information to tell init() that the received parameter is a 2D array and has a width of 10. In this case, the program can reference to the correct element within the array when you use array[i][j].
Hope this helps. :)
I understand that.
THX.:D
in fact, most compilers simply ignore the first one too even if you do specify it.Quote:
Only the last size must be fixed for 2D array.
Just felt compelled to butt in here and say: Use a vector.
typedef std::vector<std::vector<int> > 2D_ARRAY;
void foo(2D_ARRAY& a2DArray)
{
}
Why "vector<std::vector<int> >" is 2 dimension array?Quote:
Originally posted by STL MAN
Just felt compelled to but in here and say: Use a vector.
typedef std::vector<std::vector<int> > 2D_ARRAY;
void foo(2D_ARRAY a2DArray)
{
}
Could u describe that in detail?
THX.:confused:
std::vector<> is a Standard Template Library (STL) vector class. By having std::vector< std::vector<int> >, 1st vector contains vectors of int.Quote:
Originally posted by Cooker
Why "vector<std::vector<int> >" is 2 dimension array?
Could u describe that in detail?
THX.:confused:
In terms of memory layout that the vector class occupies, I don't think it will be the same as a 2D array. However, when you dereference the element within the vector object, the syntax is exactly the same, i.e. int result = array[i][j].
Could u illustrate vector< vector<T> >?Quote:
Originally posted by Kheun
std::vector<> is a Standard Template Library (STL) vector class. By having std::vector< std::vector<int> >, 1st vector contains vectors of int.
In terms of memory layout that the vector class occupies, I don't think it will be the same as a 2D array. However, when you dereference the element within the vector object, the syntax is exactly the same, i.e. int result = array[i][j].
I couldn't understand that.
Would some example?
THX.
:D
Code:#include <vector>
using namespace std;
int main(void)
{
vector< vector<int> > array(2); // Starting with 2 rows
// Populating each element
int i, j;
for(i=0; i<5; ++i)
{
array[0].push_back(i);
array[1].push_back(i+5);
}
// Display the array
for(j=0; j<array.size(); ++j)
{
for(i=0; i<array[j].size(); ++i)
printf("%d ", array[j][i]);
printf("\n");
}
return 0;
}
Just to add some detail. For a multidimensional array, all bounds other than the first one need to be fixed.Quote:
Originally posted by CBasicNet
Only the last size must be fixed for 2D array.
void init(int array[][10]);
So for a 3D array, the method signature will look like
void init(int array[][10][10]);
Kheun is right. Multidimensional arrarys are actually stored unidimensionally. So the compiler needs to know the size of each element of that unidimensional array. It is required for pointer arithmetic. For eg. operations such as array++(this will advance the array pointer by one row and not one element) need to know the size of each element.
void init(int *array);Quote:
Originally posted by tarun_jp
Just to add some detail. For a multidimensional array, all bounds other than the first one need to be fixed.
So for a 3D array, the method signature will look like
void init(int array[][10][10]);
Kheun is right. Multidimensional arrarys are actually stored unidimensionally. So the compiler needs to know the size of each element of that unidimensional array. It is required for pointer arithmetic. For eg. operations such as array++(this will advance the array pointer by one row and not one element) need to know the size of each element.
void init(int *array[]);
Could I use above to pass an aray?
THX.
Code:void init(int* array)
{
int count=0;
for(int i=0;i<10;++i)
for(int j=0;j<10;++j)
*(array+ i*10 + j) = count++;
}
int main(void)
{
int num[10][10];
init(&num[0][0]);
for(int i=0;i<10;++i)
{
for(int j=0;j<10;++j)
{
printf("%d ", num[i][j] );
}
printf("\n");
}
return 0;
}
void init(int array[][10];Quote:
Originally posted by Kheun
Code:void init(int* array)
{
int count=0;
for(int i=0;i<10;++i)
for(int j=0;j<10;++j)
*(array+ i*10 + j) = count++;
}
int main(void)
{
int num[10][10];
init(&num[0][0]);
for(int i=0;i<10;++i)
{
for(int j=0;j<10;++j)
{
printf("%d ", num[i][j] );
}
printf("\n");
}
return 0;
}
void init(int *pArray);
Which is common use in practice?
What's advantage of them?
THX.
Unless you have a good reason to use a C-style array, you
should use a vector.
Usually I would use array for pod unless there is a need to resize the array. Correct me if I am wrong. I think the overhead to use vector is too large for static pod array.Quote:
Originally posted by souldog
Unless you have a good reason to use a C-style array, you
should use a vector.
forQuote:
Originally posted by Cooker
void init(int array[][10];
void init(int *pArray);
Which is common use in practice?
What's advantage of them?
THX.
You have to pass the max number of columns and rows as well but rows can be optional if you wish.Code:void init(int *pArray);
The advantage of this form over the first form void init(int array[][10]);, is number of columns need not be fixed; ie for void init(int array[][10]);, the 2D array which you pass in, its column must always be 10.Code:void init(int *pArray, const int MaxRow, const int MaxColumn);
However, with these 2 ways of passing arrays, you have to take care not to use a array index which is out of bounds.
I would suggest you, as Souldog did, to learn to use std::vector.
Vector Tutorial
Not sure if my approach is acceptable. When it comes dynamically allocated multiple dimensional array, e.g. image processing. I usually allocated them as a 1D array and indexed it using something like *(array + i*width * j). Any comments are appreciated.
Some people do do it this way, for instance, me. It is always a hassle to dynamically allocate a multi-dimensional array.Quote:
Originally posted by Kheun
Not sure if my approach is acceptable. When it comes dynamically allocated multiple dimensional array, e.g. image processing. I usually allocated them as a 1D array and indexed it using something like *(array + i*width * j). Any comments are appreciated.
Certainly if you find yourself dynamically allocating arrays, then
it is definitely time to use a vector.
As for C-style arrays that do not change size, I would still use a
vector. The code is more maintainable and less error prone.
In this case it is especially true that there is no significant
overhead in using a vector.
Thanks CBasicNet and souldog.
I agree that vector takes care of allocating and releasing memory. For better or worst, it doesn't perform bound-checking. :(Quote:
Originally posted by souldog
The code is more maintainable and less error prone.
you can access elements of the vector with the function
at(index). which will throw a range error exception if index is out of range.
You're right. Thank!
Strangely it doesn't throw in operator[]. :confused:
Edit :
May be the C++ operator[] doesn't allow throwing? Any reason for that?
Well there are conditions under which you don't want to
throw an exception.
Error checking reduces performance and the STL was designed
for performance (This is one reason why the destructors of the
containers are not virtual).
What I have read on the matter is it was decided not to include
error checking in all operations because then you could not
write applications without error checking. Of course you
can always add error checking by writing your own wrapper.
souldog, thank very much for your informative description. :)