473,395 Members | 1,393 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,395 software developers and data experts.

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 3995
Robert Rota <se*****@csfrontiers.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***********@physik.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
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...
5
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...
4
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...
20
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)...
4
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
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...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
4
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...
9
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'...
4
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.