Path: utzoo!dciem!array!colin From: colin@array.UUCP (Colin Plumb) Newsgroups: comp.lang.c Subject: Re: Pointer Problems Message-ID: <565@array.UUCP> Date: 20 Aug 90 22:03:33 GMT References: Organization: Array Systems Computing, Inc., Toronto, Ontario, CANADA Lines: 52 In article gh1r+@andrew.cmu.edu (Gaurang Hirpara) writes: > I got a variety of solutions, but perhaps I didn't state the problem clearly. > The form ((StructName *)Something)->x=100 works. > However, the format I need is this type: > > TopLevel->Secondary->Something->x=100; > > i.e. I can't do the casting in the middle of the dereference. It won't let > me no matter which way I do it. Toplevel contains a pointer to > Secondary, which contains Something. Something initially points to a > void *. The way you've described it in english: Toplevel points to Secondary, a structure. (i.e. Toplevel = &Secondary) Something is a field of Secondary. (i.e. Secondary.Something is legal) Something is a pointer to a pointer to void. (i.e. void ** Something) StructName is a typedef for a structure with a field x, with numeric type. You want to assign that field the value 100. So you with to cast that void ** to a StructName *. Easily done: ((StructName *)Toplevel->Something)->x = 100; > I want to be able to make it point to, say a struct StructName which has > as one of its elements x, and be able to change that element. However, > if I allocate space for Something by casting to StructName, it still has > problems getting to x. I don't quite follow. Given the above and StructName foo; you can do: Toplevel->Something = (void **)&foo; ((StructName *)Toplevel->Something)->x = 100; >Hope this makes the problem clearer. Except for the fact that your sample code, Toplevel->Secondary->Something->x = 100; doesn't match the english. This has the structure Toplevel is a pointer to a structure, with a field "Secondary". Toplevel->Secondary is a pointer to a strucutre with a field Something. Toplevel->Secondary->Something is a pointer to a strucutre with a field x. Toplevel->Secondary->Something->x is a numeric variable of some sort. Now, if Toplevel->Secondary->Something is actually a void * (generaic pointer), and the structure which contains x is "StructType" then you'd cast it like so: ((struct SomeType *)Toplevel->Secondary->Something)->x = 100; Does that clear things up? -- -Colin