I have this code, and I need to understand it better. I'm hoping one of you will explain, ESPECIALLY the part using the double pointer:
Code:
typedef strudt{ // I get this part, I include it because it might help with the rest
float x, y;
float z;
flozt r, g, b;
} Vert;
typedef struct {
int n;
Vert *verts; //ok, this is for an array of verts
}Poly;
// ~~~~here is where I'm a little confused: ~~~
Poly *readPoly(FILE *f){ //how do I use this in main, or what would it look like in C++?
int i;
Poly *poly = (Poly *) malloc(sizeof(Poly)); //dynamic allocation of an array?
fscanf(f, "%d", &poly->n); //c version of reading in a file?
poly->verts = (Vert*) malloc(poly->n*sizeof(Vert)); //another dynamic allocatin of an array?
for(i=0; i< poly->n; i++)
fscanf(f, "%f %f %f %f %f %f", &poly->verts[i].x, &poly->verts[i].y /*etc?*/ );
return poly;
}
//ok, here is main:
int main(void){
int i, numPolys; // total number of poly's
Poly **polys; //array of polys ... I don't understand pointers to pointers
scanf("%d", &numPolys); //c stuff is so weird to me
polys = (Poly **) malloc(numPolys*sizeof(Poly*)); //why make another array here?
for (i = 0; i < numPolys; i++) //start reading in the files, correct?
polys[i] = readPoly(f); // f should be declared as the file name, correct? because it gives an error now
return 0;
}
I'm trying to understand the entire part from Poly *readPoly(FILE *F){ ... on. C programmers write input files differently, and I've been looking in a C book that isn't very helpful with this particular style of writing C code. I know it looks basic for C programmers, but it doesn't look baisc for a C++ programmer.
Thank you for your patience