Re: Incrementing a void pointer's member
From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 04/14/04
- Next message: Joona I Palaste: "Re: OT"
- Previous message: Dan Pop: "Re: [OT] Re: nesting coments"
- In reply to: John Leslie: "Incrementing a void pointer's member"
- Next in thread: John Leslie: "Re: Incrementing a void pointer's member"
- Reply: John Leslie: "Re: Incrementing a void pointer's member"
- Reply: CBFalconer: "Re: Incrementing a void pointer's member"
- Reply: Guillaume: "Re: Incrementing a void pointer's member"
- Reply: Dan Pop: "Re: Incrementing a void pointer's member"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 14 Apr 2004 17:44:19 GMT
John Leslie <jleslie@ptah.u.arizona.edu> scribbled the following:
> Hi,
> If I have:
> struct foof {
> long x;
> };
> void blarg(void)
> {
> struct foof blarg = {0};
> void *vp = &blarg;
> /* How do I then here increment vp->x, without using a
> struct foof *? */
> }
Is this a homework question? For some reason people tend to have a lot
of homework in the form "how can I do X without using Y".
You're in luck, as x is the first member of struct foof. Try this:
long *lp = vp;
(*lp)++;
If you're trying to access a member of the struct that is *not* the
first member, there's no portable way to do it without using a struct
pointer, because of padding.
-- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "Ice cream sales somehow cause drownings: both happen in summer." - Antti Voipio & Arto Wikla
- Next message: Joona I Palaste: "Re: OT"
- Previous message: Dan Pop: "Re: [OT] Re: nesting coments"
- In reply to: John Leslie: "Incrementing a void pointer's member"
- Next in thread: John Leslie: "Re: Incrementing a void pointer's member"
- Reply: John Leslie: "Re: Incrementing a void pointer's member"
- Reply: CBFalconer: "Re: Incrementing a void pointer's member"
- Reply: Guillaume: "Re: Incrementing a void pointer's member"
- Reply: Dan Pop: "Re: Incrementing a void pointer's member"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|