I want to simplify the following code in C. Is there any hash table in C to make it simple? Fo开发者_StackOverflowr example "dict" in Python.
int a, b, c, d ......
a = get_value_from_sth( A_NAME )
b = get_value_from_sth( B_NAME )
c = get_value_from_sth( C_NAME )
d = get_value_from_sth( D_NAME )
......
No, C does not have a built-in hash table type like Python's dicts. You may be able to get by with an array, depending on your needs.
Check out glib hash tables. Not "official" or "built in," but widely used and as close as you can get to a standard hash table implementation for C.
You will need to create a function to map a ptr to a value in an array.
This is how python does it. http://docs.python.org/c-api/dict.html
Personally I do not bother. It's C. The best solution will still be ugly.
精彩评论