Create structures dynamically

L

lancer6238

Hi,

Is there a way to create structures dynamically? For example, I could
read from a text file the data type of each member in the structure
(perhaps using numbers to represent each type - 1 for int, 2 for char
etc), the name of the members, and how many of each type. It would
even be better if I can specify, say, 4 bits of an unsigned int is for
memberA, and 4 bits of the same unsigned int is for memberB (just like
the unsigned int ip_v:4 and unsigned int ip_hl:4 in netinet/in.h).

An example would be

input text file:

DynamicStructure
1 MemberA 1 0
2 MemberB 3 0
3 MemberC1 1 4
3 MemberC2 1 4

would create the following equivalent structure named
"DynamicStructure":

struct DynamicStructure
{
int MemberA;
char MemberB[3];
unsigned int MemberC1:4;
unsigned int MemberC2:4;
}

Thank you.
 
G

Guest

Hi,

Is there a way to create structures dynamically? For example, I could
read from a text file the data type of each member in the structure
(perhaps using numbers to represent each type - 1 for int, 2 for char
etc), the name of the members, and how many of each type. It would
even be better if I can specify, say, 4 bits of an unsigned int is for
memberA, and 4 bits of the same unsigned int is for memberB (just like
the unsigned int ip_v:4 and unsigned int ip_hl:4 in netinet/in.h).

An example would be

input text file:

DynamicStructure
1 MemberA 1 0
2 MemberB 3 0
3 MemberC1 1 4
3 MemberC2 1 4

would create the following equivalent structure named
"DynamicStructure":

struct DynamicStructure
{
   int MemberA;
   char MemberB[3];
   unsigned int MemberC1:4;
   unsigned int MemberC2:4;

}

no. Not in a statically typed language like C. You'll to write
something that builds and decodes the structures on the fly.
If you need to do this a lot in your application maybe consider
a more dynamic language.
 
D

dfighter

Hi,

Is there a way to create structures dynamically? For example, I could
read from a text file the data type of each member in the structure
(perhaps using numbers to represent each type - 1 for int, 2 for char
etc), the name of the members, and how many of each type. It would
even be better if I can specify, say, 4 bits of an unsigned int is for
memberA, and 4 bits of the same unsigned int is for memberB (just like
the unsigned int ip_v:4 and unsigned int ip_hl:4 in netinet/in.h).

An example would be

input text file:

DynamicStructure
1 MemberA 1 0
2 MemberB 3 0
3 MemberC1 1 4
3 MemberC2 1 4

would create the following equivalent structure named
"DynamicStructure":

struct DynamicStructure
{
int MemberA;
char MemberB[3];
unsigned int MemberC1:4;
unsigned int MemberC2:4;
}

Thank you.
Hi!
Yes you can, but it requires lots of work.
You need to store the number of members, the type of the members and a
generic array of pointers to your datas in each instance of your
structure. You also need to dynamically allocate memory for those
members. I've recently requested comments on my current learning project
(and posted the full source code) that does something similar. You might
get some idea from that for how and what to do. Also I suggest you read
the comments from other members of the newsgroup, since they are valid
and good points. Good luck in your project!


dfighter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top