Path: 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: Reply-To: peter@ficc.ferranti.com (Peter da Silva) Organization: Xenix Support, FICC Lines: 42 In article 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?"