[c]uthash

2016-10-14 写技术

An example about uthash

#include "uthash.h"
#include <stdlib.h>   /* malloc */
#include <stdio.h>    /* printf */

typedef struct _user_t {
    int id;
    char name[25];
    UT_hash_handle hh;
} USER_T,*PUSER_T;

void main()
{
    int i;
    PUSER_T user, users=NULL;

    /* create elements */
    for(i=0; i<10; i++) {
        user = (PUSER_T)malloc(sizeof(USER_T));
        if (user == NULL) {
            exit(-1);
        }
        user->id = i;
        sprintf(user->name,"Hello, nicholas. %d", i);
        HASH_ADD_INT(users,id,user);
    }

    for(user=users; user; user=(PUSER_T)(user->hh.next)) {
        printf("user %d, name %s\n", user->id, user->name);
    }
    return 0;
}

标签: C

发表评论:

Powered by anycle 湘ICP备15001973号-1