Link to home
Start Free TrialLog in
Avatar of libin_v
libin_vFlag for India

asked on

declaring array of double pointers

Please help me in declaring an array of pointer to a pointer (double pointers) of any data type.

I figured out that in
int **a[10];
a is a pointer to a array of pointers.
Avatar of VolatileVoid
VolatileVoid

What do you mean "of any data type"?
int **a[10] is "declare a as array of (10), pointer to pointer to int"
int **a[10];
is an array of "pointers to pointers_to_int"
Avatar of libin_v

ASKER

Volatile void , by any data type i meant char or an int.
Say we are considering int


jaime
i check it out,
a is a pointer to an array of int_pointer
Avatar of Kent Olsen

int  a;    /*  integer a  */

int *a;   /*  pointer to an integer array (or pointer to an integer -- length can be 1)  */
int a[];  /*  pointer to an integer array (or pointer to an integer -- length can be 1) */

int **a;  /*  pointer to a pointer to an integer (or pointer to a pointer to an integer array -- length can be 1)  */
int *a[];  /*  pointer to a pointer to an integer (or pointer to a pointer to an integer array -- length can be 1)  */

Every time you add a '*' you add another 'pointer to ' to the description.


Kent
It's all a matter of how you read it. In this case, cdecl is quite helpful:
cdecl> explain int (*a)[]
declare a as pointer to array of int
cdecl> explain int *a[]
declare a as array of pointer to int

Notice the parenthesis.
Avatar of libin_v

ASKER

kent,
in int *a[]
a is an array of int_pointers, its is not the way u say, due to the precedence
Avatar of libin_v

ASKER

yes volatilevoid,

when i say
cdecl> explain int .....
it has to return me
declare a as array of pointer to pointer to int
#include<iostream>
#include<conio.h>


using namespace std;

int main()
{
int **a[10];
int *b[10];
// assign *b[i] to 10 different integer integer.
int c[10]={1,2,3,4,5,6,7,8,9,0};
for (int j=0;j<10;j++)
b[j]=&c[j];

for (int i =0;i<10;i++)
a[i]=&(b[i]);

for (int i =0;i<10;i++)
cout<<**a[i];
 getch();
 return 0;  
}
this is what you want?
In that case it's int **a[];
a is an array OF pointer to pointer to int.

If I had int (**a)[]; then a is pointer to pointer to array of int.
Parenthesis have higher precedence.
ASKER CERTIFIED SOLUTION
Avatar of VolatileVoid
VolatileVoid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>pointer to a pointer (double pointers) of any data type.

Check out the following links:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.cpp

Using the above Allocate2DArray function, you can create a pointer to a pointer of any type dynamically.
Example code:

int x = 4;
int y = 6;

float **My2DFloat = ALLOCATE2DARRAY(float, x, y);
int**My2DInt = ALLOCATE2DARRAY(int, x, y);

// You can also free the memory using a single wrapper function.

Free2DArray(My2DFloat);
Free2DArray(My2DInt);