Pointer to Array of 10 NodeLists? OR Array that contains Pointers to 10 NodeLists Objects -
struct nodelist * headnodeptrlist; headnodeptrlist= new nodelist[10];
is above pointer array of 10 nodelists objects? or
array contains 10 pointers 10 nodelists objects
headnodeptrlist[list].headoflist=newnode;
when try above works, if
headnodeptrlist[list] -> headoflist=newnode;
base operand of ‘->’ has non-pointer type ‘nodelist’. understand saying thought top 2 lines created array of 10 nodelist pointers
struct nodelist * headnodeptrlist; headnodeptrlist = new nodelist[10];
is above pointer array of 10 nodelists objects? or array contains 10 pointers 10 nodelists objects
it none of them. headnodeptrlist
a pointer location have reserved 10 * sizeof(nodelist)
bytes of memory.
when accessing single element array, compiler takes base address of pointer headnodeptrlist
, adds x
times sizeof(nodelist)
no address. here mean:
headnodeptrlist[4].int_member = 1234;
is same as:
*(headnodeptrlist + 4 * sizeof(nodelist)).int_member = 1234;
explaination:
a simple array my_array[3]
of struct my_struct
struct my_struct { int a; int b; }
will arranged in (example) memory:
| memory address | reserved | --------------------------------------------------------------------- 0x0000 my_array[0].a , my_array pointing @ 0x0004 my_array[0].b 0x0008 my_array[1].a 0x000c my_array[1].b 0x0010 my_array[2].a 0x0014 my_array[2].b
as access e.g. my_array[1].b
compiler create address reads address my_array + 1 * sizeof(my_struct) + offsetof(my_struct, b)
offsetof()
macro delivers offset of struct member b
inside struct my_struct
.
the error
base operand of ‘->’ has non-pointer type ‘nodelist’
means what's left of ->
not pointer. in fact dereference pointer headnodeptrlist
brackets []
.
headnodeptrlist[list] -> headoflist = newnode;
should be
headnodeptrlist[index].headoflist = newnode;
btw: don't chose name list
index variable! name them index
or indexlist
or ..
Comments
Post a Comment