By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,611 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,611 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

--
Er*********@sun.com

Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.