473,416 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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
1 1756
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.