473,788 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c struct help

Hello,

Please help me with the following struct development:

I need three levels of a data structure simulating a 3 level
pagetable. I need a structure which has a pointer to an array of
pointers. The array size will be inputted as a command line arg. These
pointers will point to another structure of the same type. Therefore
we will have a pointer to an array of ptrs each pointing to an array
which is of a different size then the first and given as another arg.
Then all of the second level ptrs must point to the same structure but
this structure has a pointer to an array (of size given as arg) of
ints. This is the last level. i think union can be used to
differentiate between the two array types? So, how do I design this
structure? How do I allocate the three levels of arrays of two types
of different sizes at runtime? I can't create all of them initially
because some of the 2nd and 3rd level array elements will not be
needed so I have to allocate them only as needed. I will create the
first level array though.

Here is my lame attempt:

typedef struct {
int level;
union PageEntry {
PAGETABLE ptrs[];
int frames[];
}
} PAGETABLE;

Any help would be greatly appreciated.

Regards
Nov 13 '05 #1
1 4025
Robert Rota <se*****@csfron tiers.com> wrote:
I need three levels of a data structure simulating a 3 level
pagetable. I need a structure which has a pointer to an array of
pointers. The array size will be inputted as a command line arg. These
pointers will point to another structure of the same type. Therefore
we will have a pointer to an array of ptrs each pointing to an array
which is of a different size then the first and given as another arg.
Then all of the second level ptrs must point to the same structure but
this structure has a pointer to an array (of size given as arg) of
ints. This is the last level. i think union can be used to
differentiate between the two array types? So, how do I design this
structure? How do I allocate the three levels of arrays of two types
of different sizes at runtime? I can't create all of them initially
because some of the 2nd and 3rd level array elements will not be
needed so I have to allocate them only as needed. I will create the
first level array though. Here is my lame attempt: typedef struct {
int level;
union PageEntry {
PAGETABLE ptrs[];
int frames[];
}
} PAGETABLE;


If I understand you're description correctly you need variables of
three types in your union - for the first level you need a pointer
to (an array of) pointers to PAGETABLE, on the next level a pointer
to PAGETABLE (which points to the first element of the array of
PAGETABLEs) and on the third a pointer to int. I shortened the
names you're using a bit:

typedef struct {
int level;
union {
struct PAGETABLE **L1;
struct PAGETABLE *L2;
int *frames;
} PE;
} PAGETABLE;
PAGETABLE pt;
int i, j;

pt.level = 1;
pt.PE.L1 = malloc( size1 * sizeof *pt.PE.L1 );

for ( i = 0; i < size1; i++ )
{
pt.PE.L1[ i ].level = 2;
pt.PE.L1[ i ].PE.L2 = malloc( size2 * sizeof *pt.PE.L1[ i ].PE.L2 );

for ( j = 0; j < size2; j++ )
{
pt.PE.L1[ i ].PE.L2[ j ].level = 3;
pt.PE.L1[ i ].PE.L2[ j ].PE.frames =
malloc( size3 * sizeof *pt.PE.L1[ i ].PE.L2[ j ].PE.frames );
}
}

Of course, you should check if malloc() did return successfully on
each step. If everything works out you can use the L1 member of the
union when you're on level 1, the L2 member when you're on level 2
and the frames member on level 3.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.physik.fu-berlin.de/~toerring
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2063
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago I was suggested by the list to use the struct module to parse it using the following code in the hypothesis that for each records I have just two fields: import struct fmt='10s1960s' size=struct.calcsize(fmt)
5
17654
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
4
5604
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can do a bsearch on the long member (nKey) but I am struggling to do a search using the string member. The code I am running appears below. It doesn't crash or anything. It is just that when I do the last bsearch using "192.168.1.3" I SHOULD find...
20
2977
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes) struct, I am storing an array of 27 pointers and a void pointer that can point to anything. typedef struct trieNode { struct trieNode *children; // The children nodes void *obj; // The object stored } TrieNode;
4
2816
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
2
2468
by: Arne Styve | last post by:
Hi, I have an API written in C that came with some graphics cards we are going to use in a project. I need to write a small application where this API is to be used, and I decided to try out C#. The API is imported by using the ; unsigned short DeviceType; unsigned long Reserved0; unsigned long Reserved1;
28
3068
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
4
2141
by: JR | last post by:
I need some help. I am trying to return a dirent struct location so i can access what the function found in main(). I dont understand pointers very well and think that is were i am getting it wrong. If i print everything from getdirlist everything is fine. I just cant pass along the namelist scandir creates. Any help would be great. JR
9
5718
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields' is there a way to treat fields as the member name of the struct?
4
2427
by: jadeivel756 | last post by:
I BADLY NEED YOUR HELP...... HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C. My problem is that as I exit the function "int reg(struct Member listm, int msize);", the data Ive entered was not stored in the array. #include <stdio.h> #include <conio.h> #include <string.h>
0
9498
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9964
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.