c - glib how address to index g_ptr_array -
i have 3 structures , use gnome glib library have dynamic array.
typedef struct neuron { //number of inputs neuron int numinputs; //growing array of weights each input garray* arrweight; }neuron; typedef struct neuronlayer { //number of neurons in layer int numneurons; //growing array of neurons gptrarray* arrneurons; }layer; typedef struct net { int numinputs; int numoutputs; int numhiddenlayers; int neuronsperhiddenlayer; //each layer of neurons gptrarray* arrlayers; }net;
i want write cycle
for (k = 0; k < net->arrlayers->pdata[i].arrneuron->pdata[j].numinputs; k++)
but dont know how address array. compiler (clang say) :
member reference base type 'gpointer' (aka 'void *') not structure or union.
structure of gptrarray:
struct gptrarray { gpointer *pdata; guint len; };
i think should write
net->arrlayers->pdata[i*sizeof(layer)].arrneurons->pdata[j*sizeof(neuron)].numinputs
gpointer
generic pointer. using store pointer of specific type. access elements must use type cast.
i first try break statement down. looks this:
net->arrlayers->pdata[i].arrneuron->pdata[j].numinputs
i assume net
variable of type net *
.
net->arrlayers
of type gptrarray *
.
net->arrlayers->pdata
of type gpointer *
, i.e. void **
, pointer pointer.
net->arrlayers->pdata[i]
of type gpointer
, i.e. void *
, pointer.
now want access element. judging context, want interpret pointer pointer layer
. however, compiler doesn't know that. need tell compiler.
so should add cast, (layer *)net->arrlayers->pdata[i]
.
now since of type layer *
, not layer
, still need ->
instead of .
access element, should become:
((layer *)net->arrlayers->pdata[i])->arrneuron
now need access pdata
again. same above, i'll skip explanation , show correct method:
((neuron *)((layer *)net->arrlayers->pdata[i])->arrneuron->pdata[j])->numinputs
as can see messy. better split several statements enhance readability, , doing may skip casts:
int current_num_inputs(net * net, size_t i, size_t j) { layer *current_layer = net->arrlayers->pdata[i]; neuron *current_neuron = current_layer->arrneuron->pdata[j]; return current_neuron->numinputs; }
then in for
loop:
for(k = 0; k < current_num_inputs(net, i, j); k++) { // ...
Comments
Post a Comment