Mostly Bollogs, I'm afraid

But occasionally, a glimmer of truth.
If you find one, please let me know.



Monday, 24 September 2012

C

I am struggling, C peeps.

I have:

struct thing {
 int i;
};

struct stuff {
 int i;
 thing *t;
};

stuff **pst;

There are many stuffs (in pst on the heap) each of which have many things which are allocated on the heap.

I know which stuff I want to access and which thing in the stuff. x is the element of thing I want.

So what I want to say is

something=*pst->(t+x)->i;

The "(" is a syntax error which it shouldn't bloody well be, but it is.

Can I access that without something like

thing *t;
t=*pst->t+x;
something=t->i;

No, didn't think so.

3 comments:

Anonymous said...

Can't you access via array syntax?

*pst->t[y]->i;

with y adjusted to an array offset from x? (x/sizeof(t)) perhaps?

SadButMadLad said...

Assuming you want pst to be a pointer to an array of many pointers to stuff structures, then the following should work.

struct stuff *somestuff = *pst[x];
struct thing *something = somestuff->t;

SadButMadLad said...

Correction

struct stuff *somestuff = pst[x];
struct thing *something = somestuff->t;