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!

Double pointer to 2D array

Status
Not open for further replies.

csripriya1

Programmer
May 13, 2003
35
US
Hi all,
I have a function which takes 2D -array as an argument. When I try to pass a double pointer as an argument ,VC 6.0 complains that the conversion is not possible.
This is how my function looks like
void slope(int a[][2],float slop[],unsigned long );
I used double pointer to dynamically allocate memory.

int **points=NULL;
points= new int* [nvertices+1];
for(i=1;i<=nvertices;i++)
points=new int [2];

this is the function call
slope(points,slop,nvertices);

Can you please help me with this.

Thanks for your time and help.
 
points=new int* [2];

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
sorry, forget first post,
function expets as argument a constant pointer.
you chould change
void slope(int** a,float slop[],unsigned long );

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
It's a very curious part of C/C++ definition: array of array types. Type of a in slope is array of array of int pairs, not int**. We can't declare or directly allocate any objects of this type because sizeof(type) is unknown. But slope prototype gets only int [nnn][2] arrays! We can:
typedef int Ipair[2];
Ipair* points = new Ipair[nvertices+1];
slope(points...
It's OK for C/C++.
Moral(?): hard use multi-dim arrays? Go to FORTRAN or learn STL containers...
 
One point is that the function signature could be described as sloppy from a C++ design standpoint.
void slope(int a[][2],float slop[],unsigned long );

At least define a structure containing the two int values per element and then use a pointer to single array of structures. Or better yet as ArkM states use the STL containers.
Code:
struct intpair{
 int a;
 int b;
};

slope( intpair* ptr, float slop[], unsigned long)

Now this still seems sloppy since you don't have any direct size or length information for both arrays. This is a nother reason to use STL containers. Bottom line, don't figure out how to pound a square peg into a round hole. Fix the design.

&quot;But, that's just my opinion... I could be wrong.&quot;

-pete
 
Hi all,
Thanks for your suggestions.I corrected my code accoringly.Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top