?
Solved

declaring array of double pointers

Posted on 2005-04-12
12
Medium Priority
?
1,068 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:libin_v
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13764746
What do you mean "of any data type"?
int **a[10] is "declare a as array of (10), pointer to pointer to int"
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13764750
int **a[10];
is an array of "pointers to pointers_to_int"
0
 
LVL 5

Author Comment

by:libin_v
ID: 13764799
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13764843

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
0
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13764868
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.
0
 
LVL 5

Author Comment

by:libin_v
ID: 13764869
kent,
in int *a[]
a is an array of int_pointers, its is not the way u say, due to the precedence
0
 
LVL 5

Author Comment

by:libin_v
ID: 13764900
yes volatilevoid,

when i say
cdecl> explain int .....
it has to return me
declare a as array of pointer to pointer to int
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13764943
#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?
0
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13764947
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.
0
 
LVL 1

Accepted Solution

by:
VolatileVoid earned 100 total points
ID: 13764964
In this case:
char **a[]; is
array of pointer to pointer to char.
and
char (**a)[] is
pointer to pointer to array of char
0
 
LVL 30

Expert Comment

by:Axter
ID: 13770799
>>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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13770809
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);
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question