473,574 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_r ply" 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_UNIT S];
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 1773
mrhicks wrote:
Hello all,

I need some advice/help on a particular problem I am having. I have
a basic struct called "indv_rpt_r ply" 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_UNIT S];
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
54436
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 { int x,y,z; char a,b,c;
20
2064
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 udpiphdr *ui; struct ip *ip; ip = (struct ip *) buf;
2
2491
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
2796
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 uiParam0; public uint uiParam1;
8
2460
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: struct pixel { int x; int y; int color; };
15
3824
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> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
12
3863
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm...
2
3258
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs...
10
2190
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; char nodes; //will be list_node1,list_node2... };
0
7730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8241
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8100
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5624
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.