Re: void * pointer convert problem.



nelu <tandauioan@xxxxxxxxx> wrote:

(IANALL; apply salt granules as appropriate.)

> struct s1 {
> int id;
> char *name;
> int year;
> };

> struct s2 {
> int id;
> char *name;
> int year;
> time_t tstamp;
> };

> ...
> void *p;
> struct s2 b;
> ...
> p=&b;
> ((struct s1 *)p)->year=2000;
> ...

> the last assignment may not be portable?

That's my reading of 6.7.2.1 (12) of n869:

"Within a structure object, the non-bit-field members and the units in
which bit-fields reside have addresses that increase in the order in
which they are declared. A pointer to a structure object, suitably
converted, points to its initial member (or if that member is a bit-field,
then to the unit in which it resides), and vice versa. There may be
unnamed padding within a structure object, but not at its beginning."

There is nothing that I can see which forbids an implementation from
putting the year members at different offsets (due to the above
mentioned unnamed padding), making the code you provided nonportable.
Of course, non DS9K machines are unlikely to behave in such a
manner...

> I may be wrong, but I think GTK+ is doing something similar.

....which is why GTK+ can probably get away with such code.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
.



Relevant Pages

  • Re: Init.c, making it chroot
    ... that's located at a special place inside the chroot. ... int devfs; ... typedef struct init_session { ... main(int argc, char *argv) ...
    (freebsd-hackers)
  • [PATCH]a tar filesystem for 2.6.10-rc1-mm3
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)
  • [PATCH] a tar filesystem for 2.6.*(easily access tar file)
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • [PATCH]a tar filesystem for 2.6.10-rc1-mm3
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)