c++ How do I use a dynamic array of pointers? -


ok, i'm trying head around pointers once start using class **c lost.

say had

struct point{ int x, y, z; }; struct polygon{ point **vertices; int size; };  point points[10]; inputpoints(points,10); //fills points array somehow  polygon square; //the following 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 point, square should hold array of pointers each reference point in points. then

square.vertices[2].x = 200; //i think done wrong 

should change points[1].x 200.

how change above code this? , while understand using std::vector better, i'm trying learn how pointers work.

you can following: (assuming vertices stores 2 point)

 point points[2];  point  p1 = {10,20,30};  point  p2 =  {20,30,50};  points[0] = p1 ;  points[1] = p2;  polygon square; //the following i'm lost square.vertices  = new point*[2]; //pay attention 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 change first point.x 100 return 0; 

you can update according needs.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -