🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3210

so how do I do it? (was Re: call to revolt)

#3210
From: minar
Date: Thu, 27 Jun 1991 05:55
50 lines
1741 bytes
If

void * p;
(int *)p++;

is illegal, how do I do what I mean?

I'm writing some code right now that needs to extract information from a buffer
that contains various types in it. Lets say that there's a chunk of memory that
I *know* first contains a char, then an unsigned. I want to get these values.

The obvious way to do it is
struct foo {
  char c;
  unsigned u;
};

and then (struct foo *)p->c  or (struct foo *)p->u

this is nonportable, as to my understanding, as struct arrangements are not
guaranteed.

The next best thing is:
*(char *)p
to get the char. Then, I want to get to the unsigned that's next, so the
obvious next step is
(char *)p++
ie: increment one character. Later, I can
(unsigned *)p++
to get past the unsigned.

This seems very natural to me. Is it illegal? If it is, what do I do instead?

(if you're curious, the structures I'm dealing with are more like:

struct foo {
  char c[x];
  int i[x];
};

where x is variable at runtime.

I don't have to write this code portably, but I'd like to write it correctly
according to ANSI anyway.

while I'm at it, how do you get the offset of an element of a structure
the ANSI way?

(please send responses to me via mail (or post, too, if you want) - I don't
always get to read news.)
Technical Details
Message-ID: <m0jspJY-0001ieC@shiva.reed.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!zephyr.ens.tek.com!tektronix!reed!minar
References: <rabson.677868220@physics.ubc.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!zephyr.ens.tek.com!tektronix!reed!minar
From: minar@reed.edu
Newsgroups: comp.std.c
Subject: so how do I do it? (was Re: call to revolt)
Message-ID: <m0jspJY-0001ieC@shiva.reed.edu>
Date: 27 Jun 91 05:55:03 GMT
References: <rabson.677868220@physics.ubc.ca>
Sender: nobody@reed.UUCP
Organization: Reed College, Portland, OR
Lines: 50