Re: Accessing members of array as a different type

Jens.Toerring_at_physik.fu-berlin.de
Date: 09/16/04

  • Next message: Derrick Coetzee: "Re: C Beginner Question"
    Date: 16 Sep 2004 19:19:02 GMT
    
    

    James A <me@privacy.net> wrote:
    > I'm having trouble geting my head around this, although I know it's a pretty
    > basic question.

    > I have an application whereby an area of RAM is mapped to an array:

    > unsigned char NV[128];

    > Two consequtive elements are used to hold an unsigned int (2 bytes on this
    > platform). How do I access it as an unsigned int?

    > e.g.
    > unsigned int test;
    > ...
    > test = *(unsigned int *)(&(NV[15]);

    > In this case, I'm trying to retrieve the unsigned int value represented by
    > elements 15 & 16 of the array. It does not seem to be working.

    It would be helpful to tell what you mean by "not seem to be working".
    That construct could, for example, crash your program with a SIGBUS
    error because of an unaligned access. Or it could work but deliver
    an unexpected result. But what you expect is something that no-one
    can figure out from what you write, so it could be that the numbers
    you're expecting in memory are stored in big-endian order while you
    try to do all of that on a little endian machine etc. Or it could
    be because what you try to read belongs actually to two different
    2-byte numbers (if, as you write, all the 128 bytes are occupied
    by 64 2-byte numbers).
                                          Regards, Jens

    -- 
      \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
       \__________________________  http://www.toerring.de
    

  • Next message: Derrick Coetzee: "Re: C Beginner Question"

    Relevant Pages

    • [patch 1/1] Hot plug CPU to support physical add of new processors (i386)
      ... The patch allows physical bring-up of new processors from facilities such as driver/utility implemented on a platform. ... +static unsigned int __devinitdata num_processors; ...
      (Linux-Kernel)
    • Re: Structure size and binary format
      ... >unsigned int b; ... >And then I'm using this as a record for a binary file. ... The simplest is to store the data as text, ... or writing custom load/save functions for each platform ...
      (comp.lang.c)
    • Re: Question about bit-fields and portability
      ... unsigned int val1: 1; ... How can such a bit-field struct be used in a non portable way? ... network) and then try to restore it on a platform with different ... or that a plain int bitfield of 1 bit can have two ...
      (comp.lang.c)
    • Re: Simple question about pointer types
      ... gazelle@xxxxxxxxxxxxxxxxxxx (Kenny McCormack) wrote: ... >>> concerned.The size of unsigned int varies from platform to platform, ... > to include an obvious qualification, which I'm sure to him seemed so ...
      (comp.lang.c)
    • Re: [OT] Argument with an OS professor over profile=3
      ... professor on the meaning of profile=3 and have been unable to convince ... The basic question is if profile=3 is passed to kernel ... unsigned int size; ...
      (Linux-Kernel)