开发者

Dynamic variable declaration in C

开发者 https://www.devze.com 2023-03-07 07:22 出处:网络
I\'m a ruby developer and its been long time since I\'ve code开发者_如何转开发d in C. I want to use a datatype in C which behaves like a symbol.

I'm a ruby developer and its been long time since I've code开发者_如何转开发d in C. I want to use a datatype in C which behaves like a symbol.

Is this possible?

  • Program asks user for name

  • User replies - "foobar"

  • Program declares an integer with the same name i.e.

    int foobar;
    


Unlike in interpreted languages, C does not have a dictionary of variable names at runtime. There exist no variable names at runtime at all. Hence unfortunately it is impossible to do what you want in C.


It's not possible to do this in C without implementing your own symbol table to emulate the desired behavior (essentially, implementing your own micro-programming language).


No. C must know names at compile time.

The best you could do is create your own dictionary of names and values. Much work though.


What do you want to do with the username-as-variable once you have it? What kind of operations would you perform with or on your foobaf variable?

As others have suggested you could use a data structure to dynamically associate the user name with a piece of integer data but knowing what you want to do with it would help inform suggestions as to whether that's even necessary and which data structures and algorithms you might want to look at.

0

精彩评论

暂无评论...
验证码 换一张
取 消