c - Using unions with structures -


i have structure this:

struct data {      char abc[10];     int cnt;     struct data *next, *prior; }; struct data *start, *last; struct data *start1, *last1; struct data *start2, *last2; 

the integer 'cnt' can have 2 values. pointers:

struct data *start, *last; 

are used link data values of 'cnt'. pointers:

struct data *start1, *last1; struct data *start2, *last2; 

are used link data when value of 'cnt' either 1 or 2. problem when change value of 'abc' or 'cnt' 1 linked list, 'start->abc', value 'start1->abc' , 'start2->abc' unchanged because live in different memory locations.
change in data under 1 list reflected in other 2 lists. believe 'unions' me don't know how set up.

any appreciated!

nope, can't done.

if come solution uses unions done, you'll have data objects allocated in such way overlap each other in memory. you'd end contiguous memory block.

rather that, disregard linked list altogether , use array:

struct data {     char abc[10];     int data; }  struct data datas[50]; struct data* = datas[20]; struct data* prev = - 1; struct data* next = + 1; 

(don't go out of bounds.)


if want linked list reason, whole point of them each element can anywhere in memory. means each element needs remember address of next , previous in order allow two-way navigation.

therefore, rather thinking union tricks, make function insertdata or removedata basic operations on list , fixes pointers in neighbouring elements.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -