0

Ok, I'm trying to get my head around pointers but once I start using something like class **c i get lost.

Say I had

struct POINT{ int x, y, z; };
struct POLYGON{ POINT **vertices; int size; };

POINT points[10];
InputPoints(points,10); //fills up the points array somehow

POLYGON square;
//the following is where I'm lost
square.vertices = new *POINT[4];
square.vertices[0] = *points[2];
square.vertices[1] = *points[4];
square.vertices[2] = *points[1];
square.vertices[3] = *points[7];

At this point, square should hold an array of pointers that each reference a point in points. Then

square.vertices[2].x = 200; //I think this is done wrong too

should change points[1].x to 200.

How would I change the above code to actually do this? And while I understand that using std::vector would be better, I'm trying to learn how pointers work.

2
  • Are you sure this line, square.vertices[0] = *points[2]; and all following three lines are working correct? points is a array of POINT and you should write it like; square.vertices[0] = &points[2]; what do you say? Commented Apr 5, 2013 at 18:26
  • Yeah, that looks better. I'll be trying that out (tacp's answer says the same). My code above was just me trying to illustrate what I wanted to do. I don't think it even compiles. Commented Apr 6, 2013 at 6:35

2 Answers 2

1

You can do something like the following: (assuming that vertices stores two point)

 POINT points[2];
 POINT  p1 = {10,20,30};
 POINT  p2 =  {20,30,50};
 points[0] = p1 ;
 points[1] = p2;

POLYGON square;
//the following is where I'm lost
square.vertices  = new POINT*[2]; //pay attention to syntax
square.vertices[0] = &points[0];  //vertices[0] stores first point
square.vertices[1] = &points[1];  //you should take address of points

square.vertices[0][0].x = 100;
std::cout << square.vertices[0][0].x 
    <<std::endl;  //this will change the first point.x to 100
return 0;

You can certainly update this according to your needs.

Sign up to request clarification or add additional context in comments.

5 Comments

ok, I think this should do what I want it to. One thing though, accessing the second point via square is done how? square.vertices[0][1].x or square.vertices[1][0].x? It seems really weird using square brackets twice to enter a 1d array, is there a way to use a & or * in conjunction with one set of [] instead?
(*vertices[0]).x seems to work in place of square.vertices[0][0].x. At least when I'm inside the struct. Not sure about externally, I guess it would be (*square.vertices[0]).x or maybe square.(*vertices[0]).x
@slicedtoad the second point should be accessed using square[1][1].x. You are actually accessing the elements of a 2D array, not 1D array, therefore, it is normal to have two [], you can think of accessing 2D array elements. Hope that this does not confuse you more.
But isn't square.vertices an array of POINT pointers? For it to be a 2D array it would need to be an array of POINT[] pointers, no? I realize that in C there is no difference between a pointer that points to an array of objects and a pointer that points to an object but square.vertices = new POINT*[2] means vertices is an array of pointers that each point to an individual object, right? Oh, and my code is working properly now, thanks. I've used your syntax except with (*vertices[0]).x instead of vertices[0][0].x.
@slicedtoad square.vertices = new POINT*[2] means vertices is an array of pointers that each point to an individual object, right? you can think of double ** vertices = new int *[2]; in this case, is vertices 2D array or 1d array? It is 2D, right, so same here. An array of pointers is means each element in the array is also a pointer that points to individual objects.
1

There are two ways to approach the solution:

  • Polygon keeps copies of the Points (this uses Point *)
  • Polygon2 keeps pointer to the Points (this uses Point **)

Following is a program with some modifications to the OP's code which exemplifies both ways. The code is also available at http://codepad.org/4GxKKMeh

struct Point { int x, y, z; };

struct Polygon {
    // constructor: initialization and memory allocation
    Polygon( int sz ) : size( sz ), vertices( 0 ) {
        vertices = new Point[ size ];
    }
    ~Polygon() {
        delete [] vertices;
        vertices = 0;
    }
    int const size;
    Point * vertices; // note: single pointer; dynamically allocated array
};

struct Polygon2 {
    // constructor: initialization and memory allocation
    Polygon2( int sz ) : size( sz ), pPoints( 0 ) {
        pPoints = new Point * [ size ];
    }
    ~Polygon2() {
        delete [] pPoints;
        pPoints = 0;
    }
    int const size;
    Point ** pPoints; // note: double pointer; points to Points :-)
};


int main() {

    Point points[10];

    // Fill up the points
    // InputPoints(points, 10);

    Polygon square( 4 );
    square.vertices[0] = points[2];
    square.vertices[1] = points[4];
    square.vertices[2] = points[1];
    square.vertices[3] = points[7];

    Polygon2 square2( 4 );
    square2.pPoints[0] = & points[2];
    square2.pPoints[1] = & points[4];
    square2.pPoints[2] = & points[1];
    square2.pPoints[3] = & points[7];
}

2 Comments

I don't quite understand this. POLYGON contains a POINT pointer. An array of type POINT is then declared at the location of this pointer. Then you seem to start filling that array with pointers. Shouldn't that be a type mismatch? I didn't think an array declared to hold a structure could hold pointers instead. But tell me if I'm missing something.
@slicedtoad: You are right, there were some syntax errors in my first version, I have fixed that and uploaded new code. Please note that there are two similar but subtly different ways to reach the solution. In my code, I have shown both the ways.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.