Skip to content

Void Pointers

Duncan Jones edited this page Nov 8, 2018 · 2 revisions

Void pointers are challenging because we have no idea what type is being referenced, so we can't automatically create a corresponding Go type and copy across the data.

To tackle this, a TOML configuration file should be supplied that specifies a mapping from each void pointer instance to a C type.

For example, this input file has two void pointers that we need to map:

typedef unsigned char *TypeB;

typedef struct {
  int val1;
  void *val2;
} TypeA;

int functionA(void *a1, long a2);

If we know that a1 is always a pointer to a TypeA and that we want to treat val1 as a TypeB, we can pass a config file to header2go containing this information:

[[voidParam]]
function = "functionA"
parameter = "a1"
replaceWith = "TypeA"

[[voidField]]
typeName = "TypeA"
field = "val2"
replaceWith = "TypeB"

In this case, we reference the C types using replaceWith = "<typedef-name>". If we need to reference a struct name instead, then we should use replaceWith = "struct <struct-name>".

A config file can have multiple voidParam and voidField tables.

Clone this wiki locally