Complicated circular type definitions: help!

M

Mark Mackey

Hi all.

I've got a rather complicated circular type definition, and would
appreciate help getting it sorted out :). Cut-down code below :

8<--------------------------------------------------------

typedef enum { SOLID, NEARESTATOM } surfcoloropt;
typedef float (*FunctionCalc)(MOLECULE *, float, int *, int *, surfcoloropt, unsigned char[4]);


/* Defines a rectangle in 3D space. We store the central point c
* the u and v axes, the number of grid points in the u and v
* directions. Note that the grid
* goes from -nu to +nu, so the total size is (2*nu+1)*(2*nv+1) points.
* Likewise, the extent of the plane in 3D is (c-u-v) to (c+u+v);
*/
struct planestruct {
int nu, nv;
p3 c,u,v;
struct planeoptionstruct *options;
struct planestruct *next;
struct planestruct *prev;
};

typedef struct planestruct PLANE;

typedef struct planeoptionstruct {
float probesize;
float clipmax,clipmin;
FunctionCalc *calc;
} PLANEOPTS;

typedef struct molstr {
//blah
//lots more blah
PLANE *plane
} MOLECULE;

8<--------------------------------------------------------

So, a MOLECULE contains a PLANE, which contains a PLANEOPTS, which
contains a FunctionCalc pointer, which takes a MOLECULE * as one
of its arguments. Is there an easy way of sorting this lot out? I can
do circular references for structs, but this one involving a function
has me confused.
 
K

Kevin Bracey

In message <P+c*[email protected]>
Mark Mackey said:
So, a MOLECULE contains a PLANE, which contains a PLANEOPTS, which
contains a FunctionCalc pointer, which takes a MOLECULE * as one
of its arguments. Is there an easy way of sorting this lot out? I can
do circular references for structs, but this one involving a function
has me confused.

Put all the typedefs and struct tag declarations first, thus:

typedef struct molstr MOLECULE;
typedef struct planestruct PLANE;
typedef struct planeoptionstruct PLANEOPTS;
typedef enum { SOLID, NEARESTATOM } surfcoloropt;
typedef float (*FunctionCalc)(MOLECULE *, float, int *, int *, surfcoloropt, unsigned char[4]);

/* Defines a rectangle in 3D space. We store the central point c
* the u and v axes, the number of grid points in the u and v
* directions. Note that the grid
* goes from -nu to +nu, so the total size is (2*nu+1)*(2*nv+1) points.
* Likewise, the extent of the plane in 3D is (c-u-v) to (c+u+v);
*/
struct planestruct {
int nu, nv;
p3 c,u,v;
struct planeoptionstruct *options;
struct planestruct *next;
struct planestruct *prev;
};

struct planeoptionstruct {
float probesize;
float clipmax,clipmin;
FunctionCalc *calc;
};

struct molstr {
//blah
//lots more blah
PLANE *plane
};

I think you've got too many "*"s on your FunctionCalc though. FunctionCalc
is actually a pointer to a function, and thus planeoptionstruct contains
a pointer to a pointer to a function.
 
M

Mark Mackey

Put all the typedefs and struct tag declarations first, thus:

Ah, that makes sense. Thanks!
I think you've got too many "*"s on your FunctionCalc though. FunctionCalc
is actually a pointer to a function, and thus planeoptionstruct contains
a pointer to a pointer to a function.

Yup, that's a bug. I suspect that the compiler would have picked that up,
except for the fact that it wouldn't compile before :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top