|
-
January 10th, 2008, 02:58 AM
#1
Passing two dimensional array in function
Hi all,
I want to pass two dimensional array in a function. The function is publicly exposed, so array length cannot be static. I want to check if the function signature is correct.
void Display(int rows, int cols, const char *const *paramValues)
{
int i;
for( i = 0; i < (rows * cols); i++ )
{
printf( "paramValues[%d] = %s\n", i, paramValues[i] );
}
}
int main( int nargs, char *args[] )
{
char *paramVals[5][2];
char a[5][10] = { "Test_1", "Test_2", "Test_3", "Test_4", "Test_5" };
char b[5][10] = { "Hello_1", "Hello_2", "Hello_3", "Hello_4", "Hello_5" };
int i;
for( i = 0; i < 5; i++ )
{
paramVals[i][0] = a[i];
paramVals[i][1] = b[i];
}
Display( 5, 2, paramVals);
return 0;
}
-
January 10th, 2008, 07:19 AM
#2
Re: Passing two dimensional array in function
Let me ask you a couple of questions:
1) Why do you access de bidimension array like this: nameArr[]. It is WRONG! First of all, it is bidimensional, and second you are using pointers (Well a const-pointer that is a reference and a pointer).
2) Why do you use a const-pointer? I mean, it isn´t bad at all, but I would use a pointer-pointing a pointer.
If you like my post, please rate it, if you don't like my way of talking, please let me know 
-
January 10th, 2008, 07:33 AM
#3
Re: Passing two dimensional array in function
Have you considered using std::string or some other string class along with std::vector or some other container?
Why do you access de bidimension array like this: nameArr[]. It is WRONG!
It is okay, especially in this context since we are dealing with an array of null terminated strings.
-
January 10th, 2008, 07:37 AM
#4
Re: Passing two dimensional array in function
 Originally Posted by laserlight
Have you considered using std::string or some other string class along with std::vector or some other container?
It would be wonderful you do that, specially when you are dealing with strings, string+vector is a power combination ^^!
 Originally Posted by laserlight
It is okay, especially in this context since we are dealing with an array of null terminated strings.
Really? I don´t know that, well, I learn something new.
If you like my post, please rate it, if you don't like my way of talking, please let me know 
-
January 11th, 2008, 12:01 AM
#5
Re: Passing two dimensional array in function
Hi,
This is an excellent thread.
When you are passing the 2-dimensional array to a function, the function declaration will atleast require to specify the second dimension size. While you pass the pointer to the 2-d array also, you may have to specify the second dimension size.
If you dont specify the size of the 2nd dimension, the array subscript missing error comes.
I listed few cases below to my knowledge:
Code:
// method 1: 2-d array
void PrintA(char a[][10])
{
for (int i=0; i<5; i++)
printf("\n%s", a[i]);
}
// method 2: 2-d array
void PrintAA(char (*a)[10])
{
for (int i=0; i<5; i++)
printf("\n%s", a[i]);
}
// method 1: Pointer to 2-d array
void PrintPVal(char *p[5][2])
{
for (int i=0; i<5; i++)
{
printf("\n%s\t%s", p[i][0], p[i][1]);
}
}
// method 2: Pointer to 2-d array
void PrintPVal(char *p[][2])
{
for (int i=0; i<5; i++)
{
printf("\n%s\t%s", p[i][0], p[i][1]);
}
}
int main()
{
char a[5][10] = { "Test_1", "Test_2", "Test_3", "Test_4", "Test_5" };
char b[5][10] = { "Hello_1", "Hello_2", "Hello_3", "Hello_4", "Hello_5" };
PrintA(a);
PrintAA(a);
char* pval[5][2];
for (int i=0; i<5; i++)
{
pval[i][0] = a[i];
pval[i][1] = b[i];
}
PrintPVal(pval);
return 0;
}
-Siva
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
|