Pointer to an array of structures, how to get it to work correclty, if possible???

Discussion in 'C Programming' started by mrhicks, Jul 16, 2004.

  1. mrhicks

    mrhicks Guest

    Hello all,

    I need some advice/help on a particular problem I am having. I have
    a basic struct called "indv_rpt_rply" that holds information for a
    particular device in our system which I will call INDV. The struct
    looks like


    // Some info used for the struct
    typedef unsigned char uint8; /* 8 bits */
    typedef unsigned short int uint16; /* 16 bits */


    struct indv_rpt_rply {
    struct generic_cmd_req header; // another struct that is common
    a
    // header routing info for each
    msg
    // that goes over the wire.
    uint8 ecbPrgmRating :8;
    uint16 voltage :16,
    current :16;
    uint8 notUsed :1,
    exist :1,
    comm :1,
    sfault :1,
    tripCause :1,
    state :3,
    ecbNum :8;
    };


    There are five Units within the system that hold different count of
    these INDV. For each Unit I have following struct.


    struct unitDev {
    uint8 numOfINDVs;
    struct indv_rpt_rply** rpt;
    };


    So for each INDV and Unit I have a declaration like


    struct idv_rpt_rply strIndvInUnit1[MAX_NUM_OF_INDV_IN_UNIT1], //
    23
    strIndvInUnit2[MAX_NUM_OF_INDV_IN_UNIT2], //
    22
    strIndvInUnit3[MAX_NUM_OF_INDV_IN_UNIT3], //
    10
    strIndvInUnit4[MAX_NUM_OF_INDV_IN_UNIT4], //
    35
    strIndvInUnit5[MAX_NUM_OF_INDV_IN_UNIT5], //
    31

    struct unitDev strUnitDev[MAX_NUM_OF_UNITS];


    This will keep track of the number of INDV within the unit and
    basically have a pointer to an array of structs. I am having problems
    equating the "pointer to an array of struct" (struct idv_rpt_rply**
    rpt) to the each of the individual INDV array. The following code will
    compile, but obvously doesn't work correctly. Is this scheme even
    possible? What would be a better approach?


    struct unitDev strUnitDev[0].rpt = (struct
    idv_rpt_rply**)&strIndvInUnit1;


    but when I test my code using Green Hills Simulator I get the
    following...


    int test;
    strIndvInUnit1[0].current = 0xAB;

    test = strIndvInUnit1[0].current; <= once executed test equates to
    0xAB;

    test = strUnitDev[0].rpt[0]->current; <= once executed test equates
    to 0x0;

    Mark
    mrhicks, Jul 16, 2004
    #1
    1. Advertising

  2. mrhicks

    Eric Sosman Guest

    Re: Pointer to an array of structures, how to get it to work correclty,if possible???

    mrhicks wrote:
    > Hello all,
    >
    > I need some advice/help on a particular problem I am having. I have
    > a basic struct called "indv_rpt_rply" that holds information for a
    > particular device in our system which I will call INDV. The struct
    > looks like
    >
    >
    > // Some info used for the struct
    > typedef unsigned char uint8; /* 8 bits */
    > typedef unsigned short int uint16; /* 16 bits */
    >
    >
    > struct indv_rpt_rply {
    > struct generic_cmd_req header; // another struct that is common
    > a
    > // header routing info for each
    > msg
    > // that goes over the wire.
    > uint8 ecbPrgmRating :8;
    > uint16 voltage :16,
    > current :16;
    > uint8 notUsed :1,
    > exist :1,
    > comm :1,
    > sfault :1,
    > tripCause :1,
    > state :3,
    > ecbNum :8;
    > };


    Just a by-the-way: The only legitimate "base types" for
    bit fields are `int' and `unsigned int' (and `_Bool' in C99).
    You should use `unsigned int ecpPrgmRating : 8' and
    `unsigned int notUsed : 1', and so on throughout the struct.
    Some compilers will accept other base types (yours clearly
    does), but they are not obliged to and some will not. In
    any case, it makes little sense to use a type like `uint8'
    for a field like `sfault' that is not eight bits wide ...

    > There are five Units within the system that hold different count of
    > these INDV. For each Unit I have following struct.
    >
    >
    > struct unitDev {
    > uint8 numOfINDVs;
    > struct indv_rpt_rply** rpt;
    > };
    >
    >
    > So for each INDV and Unit I have a declaration like
    >
    >
    > struct idv_rpt_rply strIndvInUnit1[MAX_NUM_OF_INDV_IN_UNIT1], //
    > 23
    > strIndvInUnit2[MAX_NUM_OF_INDV_IN_UNIT2], //
    > 22
    > strIndvInUnit3[MAX_NUM_OF_INDV_IN_UNIT3], //
    > 10
    > strIndvInUnit4[MAX_NUM_OF_INDV_IN_UNIT4], //
    > 35
    > strIndvInUnit5[MAX_NUM_OF_INDV_IN_UNIT5], //
    > 31
    >
    > struct unitDev strUnitDev[MAX_NUM_OF_UNITS];
    >
    >
    > This will keep track of the number of INDV within the unit and
    > basically have a pointer to an array of structs. I am having problems
    > equating the "pointer to an array of struct" (struct idv_rpt_rply**
    > rpt) to the each of the individual INDV array. The following code will
    > compile, but obvously doesn't work correctly. Is this scheme even
    > possible? What would be a better approach?
    >
    >
    > struct unitDev strUnitDev[0].rpt = (struct
    > idv_rpt_rply**)&strIndvInUnit1;


    The fact that it wouldn't compile without the cast is
    a STRONG hint that the code is wrong. Let's get back to
    first principles (and I'm going to simplify the structs
    and change their names, in pursuit of clarity). You've
    got a "parent" struct that contains a pointer to an array
    of "child" structs, each of which in turn contains a pointer
    to an array of "grandchild" structs. Here are the declarations:

    struct grandchild { ... };
    struct child { struct grandchild *gkids; ... };
    struct parent { struct child *kids; ... };

    The next thing you need is some "grandchild" arrays for the
    "child" structs to point to:

    struct grandchild gkids0[23];
    struct grandchild gkids1[22];
    struct grandchild gkids2[10];
    ...

    Now you need the array of "child" structs that point to them,
    and you need to initialize the pointers (and perhaps other
    struct elements, too):

    struct child kids[] = {
    { gkids0, ... },
    { gkids1, ... },
    { gkids2, ... },
    ...
    };

    Finally, you need the "parent" and you need to initialize
    its pointer to the "child" array:

    struct parent bigdaddy = { kids, ... };

    This pattern could be carried out to an arbitrary number
    of levels, if needed.

    --
    Eric Sosman, Jul 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Münnich

    Pointer to an array of structures

    Frank Münnich, Jul 11, 2003, in forum: C Programming
    Replies:
    9
    Views:
    41,504
    prati
    Aug 2, 2012
  2. tweak
    Replies:
    14
    Views:
    2,778
    Eric Sosman
    Jun 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  4. nospam
    Replies:
    2
    Views:
    312
    Barry Schwarz
    Oct 29, 2006
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page