Re: Incrementing a void pointer's member

From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 04/14/04


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


Relevant Pages

  • Re: structure address = structure first field address ?
    ... "everywhere except before the first member" ... "padded before struct", it is still incorrect. ... The structure must be aligned and padding inserted as needed ... system where int must be aligned on a multiple of 4 and long on a ...
    (comp.lang.c)
  • Re: structure address = structure first field address ?
    ... We can agree padded before first member to longest basic type. ... implement a struct with char followed by int, ... The structure must be aligned and padding inserted as needed ... system where int must be aligned on a multiple of 4 and long on a ...
    (comp.lang.c)
  • Re: Reinitializing static array???
    ... > work in the first member in mystruct_st itself is a struct. ... The lark is exclusively a Soviet bird. ...
    (comp.lang.c)
  • Re: Struct casting
    ... The only way this could go wrong is if the struct contains padding before ... the "bar" member MUST be the first member of the ...
    (comp.lang.c)
  • Re: Implementing JAVA interfaces in FORTRAN 20xx
    ... For C struct, it is guaranteed that a pointer to a struct ... is equivalent to a pointer to its first member. ... C interoperability <snip> ...
    (comp.lang.fortran)