Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put pointers into an array

Status
Not open for further replies.

greathope123

Programmer
Nov 1, 2011
84
GB
Hi,

I am playing pointer array but do not understand what is going on.

This pointer array *d[] looks correct:
int a1[2],b1[3];
int *d[]={a1,b1};
d[0][1]=1; d[1][2]=2;
cout<<d[0][1]<<", "<<d[1][2]<<endl;

Why this pointer array **d[] is not correct:
int a2[1][2],b2[3][4];
int **d[]={a2,b2};

I will be appreciated for any help!
 
Let's consider the declaration:
Code:
int **d[] = ...
It's an array of pointers to pointer to int. In other words, an element of a is a pointer to pointer (to int).
However
Code:
int a1[1][2]
declares an array of arrays of two ints. In other words, it's possible to convert a1 to a pointer to an array (of 2 ints), but not in a pointer to pointer.

Analogous case
Code:
int b2[3][4]
declares an array of arrays of four ints. Have you noticed that both a2 and b2 (and elements of d) have different types?

So it's impossible to initialize d by different types values a2 and b2.
 
Thank you.
I tried the same type:
int a[1][2];
int **b[1];
b[0]=a;
but still got error........
 
Of course, you got exactly the same type error - reread my prev post again (and again;). Reread your C text-book (arrays part)...

That's true type of array b:
Code:
int a[1][2]; // array of arrays of 2 ints
typedef int ArrayOf2ints[2];
ArrayOf2ints* b[1]; // compare with different type int** b[1]
b[0] = a; // that's OK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top