473,756 Members | 6,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defining the fields of a structure at run-time

I am facing this problem....
I have to define a structure at runtime as the user specifies...

The user will tell the number of fields,the actual fields...(maybe
basic or array types or multiple arrays,etc)

I do not understand how to define the structure at run time.i.e.what
fields it will contain.
Creating the field variables at run time is fine...but defining the
structure so as to contain these fields is where the problem lies...

I thought of unions within the structure representing each possible
field variable...
but again only one of these unions can exist at time...array of unions
again similar problem.

Any possible solution occuring to any one...please do let me know as
early as possible.
Thanks a lot...

Nisha.

Feb 8 '06
10 2131
stathis gotsis wrote:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:mq******** ****@news.flash-gordon.me.uk...
<snip>
Impressive, i had no idea about this!


That is what we are here for, increasing peoples knowledge :-)
So what you have to do is the reverse of when you put the data in. I.e.:
memcpy(i, a+member_offset[0], member_size[0]);
printf("Integer is: %d\n", i);
Ideally you should add at least basic error trapping, i.e.
if (member_size[0] != sizeof i)
fputs("ERROR: Size miss-match!\n");
else {
memcpy(i, a+member_offset[0], member_size[0]);
printf("Integer is: %d\n", i);
}


All is perfectly clear now, but why the error checking since i should have
explicitly set member_size[0] to sizeof(i) earlier in the program anyway?


It's called defensive programming. Although you believe that the size is
always explicitly set, there is always the possibility of error. Such as
your assumption that an int is 2 bytes! It also helps to catch something
having gone wrong corrupting the contents of member_size.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Feb 10 '06 #11

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

Similar topics

4
2175
by: Gleep | last post by:
Hi PHP coders, I've got an issue I'm stuck with. Imagine there is a large form that has 5 columns and 20 rows. In each row there is a check box - then 4 input fields. I already have the code that inserts all the data BUT the validation is a nightmare. What I need is. If an entire row is empty (not checked or filled out) that's OK. However if a user fills in one or two input fields within a row - that's NOT ok. The entire row needs...
1
19181
by: Stephan | last post by:
Hi, I'm using Visual Studio 2003 (C#) with the integrated Crystal Report software and have the following question: How can I assign a value (string) to an unbound (string) field in Crystal Report at runtime? Example: private void button1_Click(object sender,
16
12054
by: Raj Kotaru | last post by:
Hello all, I recently came across the following segment of code that defines a C struct: typedef struct { unsigned char unused_bits:4; unsigned char wchair_state:2; } xyz;
5
38370
by: mblatch | last post by:
Another basic C# question, but haven't figured out how to do this one either. Am used to defining a list of structures in C++, but am unsure if you can do this in C#. As a simplified example, I have: struct Command { int commandEnum; string commandName; int commandLength; byte command;
3
5175
by: Obrecht | last post by:
Hi. I am new to VB .NET and am just starting to do some testing with it. I am trying to define a fixed length array of structures within a structure. I pass this structure to a Win32 Btrieve API. I have one main structure and within that I need to have a fixed length array of 200 other structures. Below - shortened versions of the structures.
6
3423
by: Charles Law | last post by:
I have the following structure (for example) that I wish to convert for use in VB.NET. What would be the best way to do it? Ideally, it would convert directly to allow access to the bit fields in the same way as in C++, but I cannot see any mention of bit field declarations in VB.NET. <structure> typedef struct _DCB { DWORD DCBlength; DWORD BaudRate; DWORD fBinary :1;
6
1817
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so directly from php. Example. Table: Categories Field1: type1 Field2: type2 Field3: type3 Field4: type4
9
5716
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?
2
1919
by: Cliff Martin | last post by:
I want to define several groups of related magic numbers. I am writing a program to parse someone else's formatted data, and they have several fields that could be set to a number of different values. Rather than hardcode a number I want to use a text to make it clear what I am comparing to. examples: FieldA 1 foo 2 bar
2
5955
by: DiAvOl | last post by:
Hello everyone, I read here and there that bit-field usage should be avoided because it's not portable. My question is why it's not portable? For example let's say I have a struct in my code with bit-fields like the following: struct bitfield_t { unsigned int val1 : 1; unsigned int val2 : 3;
0
9303
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9679
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9541
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
8542
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
7078
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
5156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3651
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
2
3141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2508
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.