hash icon indicating copy to clipboard operation
hash copied to clipboard

i have a problem when i use hash_set

Open lixiaocan opened this issue 8 years ago • 0 comments

sorry,i'm a newbie,Who can tell me the problem of following code? //code

int main(int args,char *argv[])
{
    hash_t *hash_tab = NULL;
    char k[32] = {0};
    char cont[8] = {0};
    int value = 0;
    hash_tab = hash_new();
    do {
        printf("input key\n");
        scanf("%s",k);
        printf("input value\n");
        scanf("%d",&value);
        if(!hash_has(hash_tab,k))
            hash_set(hash_tab,k,&value);
        printf("conitinue add key? [y|n]\n");
        scanf("%s",cont);
    }while(!strcasecmp("y",cont));
    hash_each(hash_tab, {
                printf("%s: %d\n", key, *(int *) val);
                });
    hash_free(hash_tab);
}

//exe: ./test input key li input value 2 conitinue add key? [y|n] y input key yan input value 3 conitinue add key? [y|n] n yan: 3 yan: 3

lixiaocan avatar Aug 23 '17 03:08 lixiaocan