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: