🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3252

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

#3252
From: Peter da Silva
Date: Fri, 28 Jun 1991 17:32
42 lines
1797 bytes
In article <m0jspJY-0001ieC@shiva.reed.edu> minar@reed.edu writes:
> 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.

Option 1: define a structure for the buffer. This will automatically handle
alignment requirements and the like.

Option 2: If the buffer is packed, or imported, you will have to step
through it byte by byte:

	char *bufp;
	unsigned u;
	char c;

	bufp = buffer;

	c = *bufp++;
	u = *bufp++ << bitsperchar;
	u |= *bufp++;

Or, if the buffer is little-endian:

	c = *bufp++;
	u = *bufo++;
	u |= *bufp++ << bitsperchar;

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

No, that's portable. Structs are guaranteed to be in increasing order. Padding
is undefined, as is byte order within a word.

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

Use the offsetof() macro.
-- 
Peter da Silva; Ferranti International Controls Corporation; +1 713 274 5180;
Sugar Land, TX  77487-5012;         `-_-' "Have you hugged your wolf, today?"
Technical Details
Message-ID: <8B8C+3C@xds13.ferranti.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!qt.cs.utexas.edu!zaphod.mps.ohio-state.edu!menudo.uh.edu!lobster!sugar!ficc!peter
References: <rabson.677868220@physics.ubc.ca> <m0jspJY-0001ieC@shiva.reed.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!qt.cs.utexas.edu!zaphod.mps.ohio-state.edu!menudo.uh.edu!lobster!sugar!ficc!peter
From: peter@ficc.ferranti.com (Peter da Silva)
Newsgroups: comp.std.c
Subject: Re: so how do I do it? (was Re: call to revolt)
Message-ID: <8B8C+3C@xds13.ferranti.com>
Date: 28 Jun 91 17:32:22 GMT
References: <rabson.677868220@physics.ubc.ca> <m0jspJY-0001ieC@shiva.reed.edu>
Reply-To: peter@ficc.ferranti.com (Peter da Silva)
Organization: Xenix Support, FICC
Lines: 42