I am looking for something like:
a["h开发者_Go百科ello"]
points to a list, a["h"]
to another.
Basically, I'm looking for an associative array implementation.
No, not in C. You may want to look into hash table libraries to get what you want.
A bit of trivia: although strings cannot typically be used as indices, in certain narrow cases, they are valid.
Consider this line of code:
printf("%c", 1["hello"]);
The output is "e".
This exploits the property that a[b] == *(a+b) == *(b+a) == b[a].
As a result, 1["hello"] == "hello"[1], which clearly results in the second letter, "e"
Not directly, but there are a number of hash table implementations in C.
Here's one example. I haven't tried it, but it was near the top of a Google search. (Presumably it doesn't let you use the a["hello"]
syntax.)
(And if using C++ rather than C is an option, something like a map
might be a good solution.)
You cannot do this exactly as written ( like you can do in python or perl ) But, If you want to use a string as an index. It sounds very much like you want a perl hash or dictionary. I have a quite lightweight dictionary/ahash implementation for C that lets you use anything as a key (index) including strings.
xrhash - guthub
Here's a brief example of using a string as an indexer:
XRHash * dict = xr_init_hash( xr_hash__strhash, xr_hash__strcmp );
/* add things to the hash */
if ( xr_hash_contains( dict, "mary" ) != XRHASH_EXISTS_TRUE )
printf("does not contain mary\n");
xr_hash_add( dict, "fred", strdup("my name is fred") );
xr_hash_add( dict, "mary", strdup("my name is mary") );
xr_hash_add( dict, "sally", strdup("my name is sally") );
xr_hash_add( dict, "bob", strdup("my name is bob") );
/* test if things exist */
if ( xr_hash_contains( dict, "mary" ) == XRHASH_EXISTS_TRUE )
printf("does contain mary\n");
/* iterate the hash */
xrhash_fast_iterator * iter = xr_init_fasthashiterator( dict );
char * hashkey = NULL;
do {
hashkey = (char*) xr_hash_fastiteratekey( iter );
if ( hashkey != NULL ){
/* get values */
char * data;
xr_hash_get( dict, (void*)hashkey, (void**)&data );
printf("key = %s, value = %s\n", hashkey, data );
} else {
break;
}
} while ( 1 );
The full source for the above is here
EDIT:
There also turns out to be Hash Tables that do the same and more from GLib. My lib is quite fast though :)
All the answers so far have focused purely on syntax, not on solving your problem. If you really need an "associative array", you need to find or write some library routines to provide such a data structure. C does not have any high-level data structures as builtin types or part of the standard library. glib
surely provides something like what you're looking for.
Note however: (!!) If you find yourself needing associative arrays outside of specialized applications where the key comes from the outside world (as opposed to within your program), I think you should really evaluate either (1) whether there's a better way to do what you're trying to achieve in C, or (2) whether you really want to be programming in C. Associative arrays are not a C idiom, and by using them when another data type would do, you're throwing out many of the advantages of C and just keeping all of the pain of C.
No, array are just pointers and the index operator ( [] ) is just a shorthand for :
a[i] == *(a + i * sizeof(*a))
It's called an std::map
and you can use std::string
as a key.
Note that this is only an option in C++, not C. However, I'm thinking it's probably what you were looking for, as many times novices will tag C questions as C++ and the other way around.
精彩评论