Dear All,
I am facing a problem in passing 2D array of pointers to a function.

can any one give some suggestions on this.

  1. How the Declaration fo the function should be ?
  2. How to pass 2D array of pointers to that function?
  3. How to access the array elements.

    The aray declaration is *ptr[2][3] = { {"AA","XX","FF"},
    {"DD","QQ","EE"},
    };

It would be great helpful for me,since I am struggling with this one

If you're struggling, duplicate your declaration:

void foo ( int *arg[2][3] );

int main ( void )
{
  int *ptr[2][3];

  foo ( ptr );

  return 0;
}

Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.

If you're struggling, duplicate your declaration:

void foo ( int *arg[2][3] );

int main ( void )
{
  int *ptr[2][3];

  foo ( ptr );

  return 0;
}

Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.

Hi,
Thanks for your reply.

But I have diffrent types of 2D array of pointers which varies rows and columns size, So in the function declaration/definition I can not specify the rows and columns size.

So I am trying for an alternative.

>So in the function declaration/definition I can not specify the rows and columns size.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:

#include <stdlib.h>

void foo ( int ***arg );

int main ( void )
{
  int ***ptr = malloc ( 2 * sizeof *ptr );
  int i;

  for ( i = 0; i < 2; i++ )
    ptr[i] = malloc ( 3 * sizeof *ptr[i] );

  foo ( ptr );

  for ( i = 0; i < 2; i++ )
    free ( ptr[i] );
  free ( ptr );

  return 0;
}

You have more options in C++, but you neglected to mention what language you're using.

here is how you initialize the function and pass the values

char *function(char  **array)
{
//your code here
return 0;
}

int main()
{

//put values in array

function(p);  //passing to function

}

Maybe you want to take a look at my code:

#include <iostream>

using namespace std;

void func(char **& ref_to_ptr); /* Function declaration */

int main(void)
{
    /* Declare the '2D Array' */
    char ** ptr = new char * [5];
    ptr[3] = new char[20];

    /* Put some data in the array */
    ptr[3] = "k";

    /* Print the first value on the screen */
    cout << "First value: " << ptr[3] << endl;

    /* Pass the array by reference to the function 'func()' */
    func(ptr);

    /* Again we print on the screen what's in the '2D Array' */
    cout << "Second value: " << ptr[3] << endl;

    /* Wait for the user to press ENTER */
    cin.get();

    /* Cleanup */
    delete[] ptr;

    /* Tell the Operating System that everything went well */
    return 0;
}

void func(char **& ref_to_ptr)
{
    /* This function demonstrates how to change the value of it's argument(s) */
    ref_to_ptr[3] = "tux4life";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.