Created
May 17, 2021 15:05
-
-
Save airportyh/10613f90e3bb1ae95409feccf8623424 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include "uthash.h" | |
typedef struct _Person { | |
unsigned long id; | |
char *name; | |
char *email; | |
UT_hash_handle hh; | |
} Person; | |
Person *persons = NULL; | |
Person *personsById = NULL; | |
int main() { | |
char *sentence = "I love Bobby Brown."; | |
Person *p = malloc(sizeof(Person)); | |
memset(p, 0, sizeof(Person)); | |
p->id = 1; | |
p->name = "Bobby"; | |
p->email = "[email protected]"; | |
// HASH_ADD_KEYPTR(hh, persons, p->name, strlen(p->name), p); | |
HASH_ADD_INT(personsById, id, p); | |
Person *p2 = malloc(sizeof(Person)); | |
memset(p2, 0, sizeof(Person)); | |
p2->id = 2; | |
p2->name = "Sarah"; | |
p2->email = "[email protected]"; | |
// HASH_ADD_KEYPTR(hh, persons, p2->name, strlen(p2->name), p2); | |
HASH_ADD_INT(personsById, id, p2); | |
// Person *p3; | |
// HASH_FIND_STR(persons, "Sarah", p3); | |
// printf("Found person: %s\n", p3->name); | |
// printf(" email: %s\n", p3->email); | |
// Person *p4; | |
// HASH_FIND(hh, persons, sentence + 7, 5, p4); | |
// printf("Found person: %s\n", p4->name); | |
// printf(" email: %s\n", p4->email); | |
Person *p5; | |
unsigned long id = 1; | |
HASH_FIND_INT(personsById, &id, p5); | |
printf("Found person by id: %s\n", p5->name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment