Skip to content

i have a problem when i use hash_set #9

@lixiaocan

Description

@lixiaocan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions