How to create a variable with a name given by the user?

Hi,

i want to create a variable, but the name and the type should be given by the user, (IN C)

so example if the user wanted. a integer called worldrecord, i should be able to create it then the variable can be used by the user.

same for character, so the user says i want to create a char with name myname and length 20
so i should be able to create that,

so it would be
char myname[20]

i am not so sure how to do it, i really need help please.

you need to dynamically allocate the memory

http://www.cplusplus.com/doc/tutorial/dynamic/

you won't be able to specify the name of the variable, but that shouldn't matter since the user won't see the name of the variable. then you could just use a switch or something for the type. "enter 1 for int, 2 for double, etc."
Sounds like you are trying to write a script interpreter. And I think that is a bit above your head right now.
what i though about was using unions, so i would define my union like this

1
2
3
4
5
6
7
8
9
typedef union 
{
   int in;
   float fl;
   char * ch
   

} create;


then when a user wanted an int of name MyMarks
i can do
1
2
3
4
create *MyMarks = allocate dynamic memory here;
and to inilize i can do MyMarks->in=99;


can you tell me if this could be a good solution? or not so great

Technically that would probably work... though I REALLY have to ask why you even WANT to do this in the first place?
beacuse what i am trying to do is this:
given a format like;

table subwayLines name:char[30], stops:int, kilometres:float

i have to first create a table name subwaylines (so a sturct)

then in the table i should have three columns or fields, one is call names and is of type char with length 30 and so on

its basically configuration file parsing for a server

also i just realized i can't create a pointer called MyMarks, since i don't know what the user would call it as,
Put your structs into a map.

http://cplusplus.com/reference/stl/map/
problem is this is in C, and not in C++,
In that case, you will have to write your own map container.
Topic archived. No new replies allowed.