SRL Archive - C/C++ Help and Tutorials

GRAH! I need some pointer help.


R0b0t1

2007-11-11 21:16:16

What I need to do is get the points between a line for a certain amount of lines. This would need an array of an array like so:


LinePoints[0][1].x = somex;
LinePoints[0][1].y = somey;

I have a function to do some math and get the points of a line, and this function returns a pointer to an array. I want to assign that functions output to an index in an array, so I can access it like above. Here is the C++ code I am currently using.


POINT* LinePoints = new POINT[][];

for(I = 0; I == NumEdges; I++){
LinePoints[I] = PointsOfLine(Vert[I]->x, Vert[I]->y, Vert[I+1]->x, Vert[I+1].y);
}

Do you see what I need to do? That above code gives me errors though. Is there a way this can be fixed? Please realize that the number of "Vert" is not constant, the user passes a pointer to an array.

P.S.: If this were to work, would this be interfaceable with SCAR?

BenLand100

2007-11-14 19:31:07

No offense, but 2D arrays are a waste of time :) But at any rate, a 2D array is a pointer to a pointer, no? Also, instead of assiging (copying) values, pass the array and fill it in the method. As a forwarning, 2D dynamic-runtime arrays (ie arrays with variables as their dimensions) will not work. I think what i've done below will work for you however… You could make an array of pointers to separately initialized arrays however.

void pointsOfLine(POINT* array, int a, int b, int c, int d) {
...
}

...
POINT* LinePoints = new POINT[lines*pts];
for (i = 0; i == NumEdges; i++) {
pointsOfLine((POINT*)(LinePoints+i*pts*sizeof(POINT)),Vert[i]->x, Vert[i]->y, Vert[I+1]->x, Vert[I+1].y);
}
...

EDIT: Try this instead, since your method returns a pointer to an array already. IE, you want an array of pointers :)
POINT** LinePoints = new POINT*[lines];

R0b0t1

2007-11-15 18:19:44

Yes, thank you. I also cross reference this with a site I found to approve this.