Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!usc!apple!olivea!orc!inews!iwarp.intel.com!psueea!eecs!griffith From: griffith@eecs.cs.pdx.edu (Michael Griffith) Newsgroups: comp.lang.c Subject: Re: Pointer Problems Message-ID: <3242@psueea.UUCP> Date: 15 Aug 90 20:21:58 GMT References: <8aliOrS00WBMI1UVYX@andrew.cmu.edu> Sender: news@psueea.UUCP Reply-To: griffith@eecs.UUCP (Michael Griffith) Organization: Portland State University, Portland, OR Lines: 35 In article <8aliOrS00WBMI1UVYX@andrew.cmu.edu> gh1r+@andrew.cmu.edu (Gaurang Hirpara) writes: >Here's a problem which has been bugging me for a while now: >Ok. I have a struct, call it idiot, with some elements in it, nothing unusual. >Now, I have another struct, which contains in it a generic >pointer (i.e. Ptr ). >How can I make point to idiot, AND be able to access the >resulting pointer as a pointer to struct. My main problem is that even >if I do manage to get them to point to the same place (which is a problem >all by itself), I can't access the internal members of the struct. I beleive that you can typecast the pointer like so: ((struct window *)generic_pointer)->width = 100; or something similar. In other words, you typecast it as belonging to the appropriate pointer type before you try to reference elements in it. You can also define generic_pointer to be a union of your other structure types and then access the appropriate structure type: union gen_ptr_type { struct window *window_ptr; struct screen *screen_ptr; . . . struct button *button_ptr; }; And then you could reference it like so: (generic_pointer.window_ptr)->width = 100; Michael Griffith griffith@eecs.ee.pdx.edu uunet!tektronix!psueea!eecs!griffith