CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2006
    Posts
    28

    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;
    }

  2. #2
    Join Date
    Jul 2006
    Location
    Buenos Aires, Argentina
    Posts
    103

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2006
    Location
    Buenos Aires, Argentina
    Posts
    103

    Re: Passing two dimensional array in function

    Quote 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 ^^!

    Quote 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

  5. #5
    Join Date
    Dec 2007
    Location
    Chennai (India)
    Posts
    25

    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
  •  





Click Here to Expand Forum to Full Width

Featured