#include <stdio.h>
#include <string.h>
struct _Phone_Entry {
const char *name;
const char *number;
};
typedef struct _Phone_Entry Phone_Entry;
static Phone_Entry _start_entries[] = {
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
{ "Ludwig van Beethoven", "+12 34 567-89101" },
{ "Richard Georg Strauss", "+23 45 678-91012" },
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
{ NULL, NULL }
};
static const char *_nicknames[] = {
"mozzart",
"betho",
"george",
"hector",
NULL
};
static void
_phone_entry_free_cb(void *data)
{
free(data);
}
{
Phone_Entry **pe = (Phone_Entry **)key;
const char *nick = data;
printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick);
}
int
{
int i;
Phone_Entry *entry_vl = &_start_entries[3];
Phone_Entry *p = NULL;
char *nick = NULL;
void *data;
for (i = 0; _start_entries[i].name != NULL; i++)
{
p = &_start_entries[i];
strdup(_nicknames[i]));
}
printf("Phonebook:\n");
printf("\n");
if (nick)
{
printf("Printing entry.\n");
printf("Name: %s\n", entry_vl->name);
printf("Number: %s\n", entry_vl->number);
printf("Nick: %s\n\n", nick);
}
printf("Hash entry successfully deleted? %d\n\n", r);
p = &_start_entries[2];
strdup("el jorge"));
free(nick);
Phone_Entry *p1 = malloc(sizeof(*p1));
p1->name = "Raul Seixas";
p1->number = "+55 01 234-56789";
strdup("raulzito"));
if (!nick)
{
printf("No previous nick found for Raul Seixas. ");
printf("Creating new entry.\n");
}
else
{
printf("Old nick for Raul Seixas was %s\n", nick);
free(nick);
}
printf("\n");
strdup("raulzao"));
if (nick)
{
printf("Changing nick for Raul Seixas to raulzao. ");
printf("Old nick was %s\n", nick);
free(nick);
}
else
{
printf("No previous nick found for Raul Seixas. ");
printf("Creating new entry.\n");
}
printf("Phonebook:\n");
printf("\n");
printf("Phonebook:\n");
{
Phone_Entry **pe = (Phone_Entry **)t->key;
nick = t->data;
printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick);
}
printf("\n");
printf("List of names/numbers in the phone book:\n");
{
Phone_Entry **pe = (Phone_Entry **)data;
printf("%s: %s\n", (*pe)->name, (*pe)->number);
}
printf("\n");
printf("List of nicks in the phone book:\n");
{
nick = data;
printf("%s\n", nick);
}
printf("\n");
printf("There are %d items in the hash.\n\n",
Phone_Entry *p2 = malloc(sizeof(*p2));
p2->name = "Alceu Valenca";
p2->number = "000000000000";
printf("List of phones after change:\n");
printf("\n");
printf("There are %d items in the hash.\n\n",
free(p1);
free(p2);
}