c - I can't figure out my pointer error -


i new c. have problem can't 2 pointers point same space in memory. here code.

struct io{     int val;     char* name; };  struct gate{     enum gatetype type;     struct gate* next;     int numinputs;     struct io* outputs;     struct io* inputs; }; 

in main have

struct gate* tempgate; tempgate = (struct gate*)malloc(sizeof(struct gate)); struct io* iolist; iolist = (struct io*)malloc(sizeof(struct io)*20);  tempgate->inputs = (struct io*)malloc(sizeof(struct io*)*2); tempgate->outputs = (struct io*)malloc(sizeof(struct io*)); 

later in nested loop have this

tempgate->inputs[j] = iolist[i]; 

now when change value of iolist[i], shouldn't tempgate->inputs[j] change well? if not why? how can make case? me codiwan you're hope.

you should make inputs , outputs arrays of pointers io, not arrays of io. can make elements of array point elelemnts of iolist.

struct gate{     enum gatetype type;     struct gate* next;     int numinputs;     struct io** outputs;     struct io** inputs; };  tempgates->inputs = malloc(sizeof(struct io*)*2); tempgates->outputs = malloc(sizeof(struct io*)); 

then loop should be:

tempgate->inputs[j] = &(iolist[i]); 

then when change iolist[i].val, change value of tempgate->inputs[j]->val.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -