- 论坛徽章:
- 1
|
本帖最后由 wujiajia 于 2010-10-14 17:15 编辑
- #include <stdio.h>
- #include <string.h>
- #include "hashit.h"
- #include "hfunctions.h"
- #define A 0
- hash_t hashtab;
- int cmp(const void *p1, const void *p2) { return strcmp((const char * ) p1, (const char * ) p2); }
- int init() {
- int i;
- #if A
- char key[][10]={"name","address","phone","door","city","sx","major"};
- char value[][15]={"netboy","china","263788","110#","guangzhou","male","information"};
- #else
- char *key[]={"name","address","phone","door","city","sx","major"};
- char *value[]={"netboy","china","263788","110#","guangzhou","male","information"};
- #endif
- char *pkey;
- char *pval;
- for(i=0;i<2;i++) {
- /* pkey=(char*)malloc(strlen(key[i])+1); pval=(char*)malloc(strlen(value[i])+1); strcpy(pkey,key[i]); strcpy(pval,value[i]); */
- //hashit_insert(hashtab,pkey,pval);
- hashit_insert(hashtab,key[i],value[i]);
- }
- printf("A name=%s\n",(char*)(hashit_lookup(hashtab,(char*)"name")));
- printf("A address=%s\n",(char*)(hashit_lookup(hashtab,(char*)"address")));
- }
- int main()
- {
- hashtab = hashit_create(5, 7,NULL,cmp,CHAIN_H);
- init();
- printf("begin...\n");
- //此处如果为空 是因为hash中保存的为指针
- printf("B name=%s\n",(char*)(hashit_lookup(hashtab,(char*)"name")));
- printf("B phone=%s\n",(char*)(hashit_lookup(hashtab,(char*)"address")));
- hashit_destroy(hashtab);
- return 0;
- }
复制代码 结果是 如果定义的A=1:
A name=(null)
A address=china
begin...
B name=(null)
B phone=(null)
反之:
A name=netboy
A address=china
begin...
B name=netboy
B phone=china |
|