Runtime Structure

C

chellappa

Hi EveryBody!
i need a information about creatting structure at runtime....
like example
.....
int main()
{
char *a,*b;
printf("Enter the structure");
scanf("%s",a);
printf ("Enter The Datatype");
scanf("%s",b);
create(a,b);
}
void create(char *F,Char *G);
{
struct F
{
G a;
}
}

i hope u will understand this program... this example ,but i need
inforamtion any possibleties to create struct and its field in runtime
, that user will define..
if posssible give me reply
Thanks
BY
CNS
 
A

Alexei A. Frounze

chellappa said:
Hi EveryBody!
i need a information about creatting structure at runtime....
like example
....
int main()
{
char *a,*b;
printf("Enter the structure");
scanf("%s",a);
printf ("Enter The Datatype");
scanf("%s",b);
create(a,b);
}
void create(char *F,Char *G);
{
struct F
{
G a;
}
}

i hope u will understand this program... this example ,but i need
inforamtion any possibleties to create struct and its field in runtime
, that user will define..
if posssible give me reply

1. To create something in runtime you first need allocate memory to it
provided you know the size of that something.
2. It is impossible to define (note, not create) something in a C program
after it has been compiled. C programs are compiled, not interpreted. I they
had been interpreted, it would probably have been possible to define
something on the go. Hence, interpretation is to be done by you...
3. Think well whether this is indeed what you need in your program. Can't
you define all what need in the program and then create one of those known
things?
4. What you can do though is...
4a. generate C source code for another program in your original program,
compile and run it -- not sure it's suitable but it's possible
4b. in your program ask the user exactly what he needs inside the structure
(what types), calculate offsets and sizes of the members so that it would be
possible to allocate enough memory for the structure and fill in all its
members at proper addresses

HTH
Alex
P.S. Consider (3) or (4b)
 
R

Richard Heathfield

chellappa said:
Hi EveryBody!
i need a information about creatting structure at runtime....

i hope u will understand this program... this example ,but i need
inforamtion any possibleties to create struct and its field in runtime
, that user will define..
if posssible give me reply

This is much harder than you think. It is, however, possible, sort of, kind
of, in a way. (Are you there, Jamison Ch? Do you remember FFD?)

But you effectively end up (almost) writing your own database engine. I
doubt very much whether you really want to go to that much effort. We are
talking several weeks of development and testing here, and at the end you
probably won't get the syntax you wanted anyway. (What you will get is the
ability to store arbitrary data, the type or types of which is decided at
runtime.)

Think very carefully before embarking on such a course.
 
J

John Bode

chellappa said:
Hi EveryBody!
i need a information about creatting structure at runtime....
like example
....
int main()
{
char *a,*b;
printf("Enter the structure");
scanf("%s",a);
printf ("Enter The Datatype");
scanf("%s",b);
create(a,b);
}
void create(char *F,Char *G);
{
struct F
{
G a;
}
}

i hope u will understand this program... this example ,but i need
inforamtion any possibleties to create struct and its field in runtime
, that user will define..
if posssible give me reply
Thanks
BY
CNS

You can't define a C struct at runtime, at least not in the way you're
expecting.

You *can* use structs and unions to create dynamic records, but it's a
bit of work, and I can't toss off an example right now. If I get a
chance tonight I'll work on a simple example.
 

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
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top