Re: void * pointer convert problem.
- From: Christopher Benson-Manica <ataru@xxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 30 Dec 2005 20:38:28 +0000 (UTC)
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.
.
- Follow-Ups:
- Re: void * pointer convert problem.
- From: Keith Thompson
- Re: void * pointer convert problem.
- From: tmp123
- Re: void * pointer convert problem.
- References:
- void * pointer convert problem.
- From: Eric J.Hu
- Re: void * pointer convert problem.
- From: Christopher Benson-Manica
- Re: void * pointer convert problem.
- From: Eric J.Hu
- Re: void * pointer convert problem.
- From: tmp123
- Re: void * pointer convert problem.
- From: Christopher Benson-Manica
- Re: void * pointer convert problem.
- From: nelu
- void * pointer convert problem.
- Prev by Date: Re: void * pointer convert problem.
- Next by Date: Re: void * pointer convert problem.
- Previous by thread: Re: void * pointer convert problem.
- Next by thread: Re: void * pointer convert problem.
- Index(es):
Relevant Pages
|