Start New Discussion within our Software Development Community

i have a struct with an array of three points in it, for example:
struct POINTS
{
POINT pts[3];
};
then I have another struct with two single points in it like this:
struct EDGE
{
POINT *p1, *p2;
}
is there a way to set p1 to a single element in the attay in POINTS?
here is an example:

POINTS some_points;
EDGE an_edge;
an_edge.p1 = some_points.pts[0];
now p1 is an array of 3 points, but I only need a pointer to pts[0],
and I need to be able to call it without providing an index.
speed is an issue, and thats why I am trying to eliminate the need for copying.
thanks

note that POINTS actually contains 3 full instances of the POINT (whatever that happens to be) while EDGE only contains 2 pointers to POINT.

This code isn't quite right:

POINTS some_points;
EDGE an_edge;
an_edge.p1 = some_points.pts[0];

This will work:

POINTS some_points;
EDGE an_edge;
an_edge.p1 = &some_points.pts[0];
// or
an_edge.p1 = some_points.pts;
// or if you wanted to point to something other than the first element in the array:
an_edge.p1 = &some_points.pts[1];
// or
an_edge.p1 = &some_points.pts[2];

In the case where p1 points to pts[0], yes it is true that there are 2 more data items following the one you're pointing to, but if p1 was pointing to pts[2] there are not. It would all be a matter of handling p1 as if it only points to one value (specifically ignore or refuse to use anything other than the one value is points to.)

Note that the assignment to p1 (or p2) does NOT copy the data in the POINT, it only copies the address of where the POINT was located in memory.

This article has been dead for over six months. Start a new discussion instead.