Repeated struct definitions for graph data structures and in/out naming conflict in C library

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 3 05:45:13 PST 2016


On 03.01.2016 14:30, data pulverizer wrote:
> I am trying to access functionality in the glpk C library using
> extern(C). It has graph structs in its header file that are specified in
> an odd recurring manner that I cannot reproduce in D:

I don't see what's odd about this. What exactly are your struggling with?

> typedef struct glp_graph glp_graph;
> typedef struct glp_vertex glp_vertex;
> typedef struct glp_arc glp_arc;

You can just drop these. http://dlang.org/ctod.html#tagspace

> struct glp_graph
> {
>       ...
>        glp_vertex **v; /* glp_vertex *v[1+nv_max];
> };

Drop the semicolon after the struct declaration, and move the asterisks 
one place to the left (purely style):

struct glp_graph
{
     glp_vertex** v;
}

> struct glp_vertex
> {
>        ...
>        glp_arc *in;
>        glp_arc *out;
> };

As above, and rename in/out to something else, e.g. in_ and out_:

struct glp_vertex
{
     glp_arc* in_;
     glp_arc* out_;
}

> struct glp_arc
> {
>        glp_vertex *tail;
>        glp_vertex *head;
>        glp_arc *t_prev;
>        glp_arc *t_next;
>        glp_arc *h_prev;
>        glp_arc *h_next;
>        ....
> };

Nothing new here.

> you may also spot that the in, and out keywords are used as members in
> the struct, which gives an error in D. These structs are required for
> functions in the library so need to be included in the D interface file.

Just rename them to something else. In D code that uses the struct, you 
use the new names. C code doesn't need to be changed, as the name 
doesn't matter when compiled.


More information about the Digitalmars-d-learn mailing list