File: test35.c

package info (click to toggle)
uthash 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,788 kB
  • sloc: ansic: 9,838; makefile: 178; perl: 88; sh: 37; cpp: 30
file content (37 lines) | stat: -rw-r--r-- 874 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "uthash.h"
#include <string.h>   /* strcpy */
#include <stdlib.h>   /* malloc */
#include <stdio.h>    /* printf */

typedef struct elt {
    char *s;
    UT_hash_handle hh;
} elt;

int main()
{
    int i;
    elt *head = NULL;
    elt elts[10];
    char label[6] = "hello";
    for(i=0; i<10; i++) {
        elts[i].s = (char*)malloc(6UL);
        strcpy(elts[i].s, "hello");
        elts[i].s[0] = 'a' + i;
        printf("%d: %s\n", i, elts[i].s);
        HASH_ADD_KEYPTR(hh, head, elts[i].s, 6UL, &elts[i]);
    }

    /* look up each element and verify the result pointer */
    for(i=0; i<10; i++) {
        elt *e;
        label[0] = 'a' + i;
        HASH_FIND(hh,head,label,6UL,e);
        if (e != NULL) {
            printf( "found %s\n", e->s);
            printf( "right address? %s\n", (e == &elts[i]) ? "yes" : "no");
        }
    }

    return 0;
}