|
-
September 24th, 2003, 09:02 AM
#1
how to pass 2-dimension array to function parameter
I have a question:
How to pass 2-dimension(or 3-dimension....) array to function?
Using pointer,array parameter?
Could someone teach me?
Thanks!!
-
September 24th, 2003, 08:29 PM
#2
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");
}
}
-
September 24th, 2003, 10:38 PM
#3
You can do like this too. Only the last size must be fixed for 2D array.
void init(int array[][10]);
-
September 24th, 2003, 10:44 PM
#4
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;
}
-
September 25th, 2003, 07:01 AM
#5
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]);
Why the last size must be fixed???
-
September 25th, 2003, 07:04 AM
#6
Coud I take a pointer type as parameter to pointer an array?
-
September 25th, 2003, 07:06 AM
#7
Else it wouldn't compile.
-
September 25th, 2003, 07:55 AM
#8
Originally posted by CBasicNet
Else it wouldn't compile.
I know it.
But I would like to know why compiler cann't compile it.
-
September 25th, 2003, 07:58 PM
#9
Originally posted by Cooker
I know it.
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.
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;
}
-
September 26th, 2003, 04:44 AM
#10
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;
}
"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."
Could u describe that clearly?
I don't know what you talked.
THX.
-
September 26th, 2003, 06:05 AM
#11
Sorry I made a mistake in my explanation. It should be
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.
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.
-
September 26th, 2003, 07:36 PM
#12
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.
Oh!
I understand that.
THX.
-
September 26th, 2003, 10:39 PM
#13
Only the last size must be fixed for 2D array.
in fact, most compilers simply ignore the first one too even if you do specify it.
-
September 27th, 2003, 02:27 AM
#14
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)
{
}
Last edited by STL MAN; September 27th, 2003 at 03:18 AM.
DEFENDER OF ALL THINGS STL!!!
-
September 27th, 2003, 03:04 AM
#15
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)
{
}
Why "vector<std::vector<int> >" is 2 dimension array?
Could u describe that in detail?
THX.
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
|