История изменений
Исправление lenin386, (текущая версия) :
# define MAXP 10
# include <stdio.h>
# include <stdlib.h>
#define _GNU_SOURCE
#define __USE_GNU
#include <search.h>
typedef struct
{
char * key;
int i;
char * s;
} S;
S s[MAXP] = {0};
struct hsearch_data * hash;
void add (char * v1, int v2, char * v3)
{
ENTRY e;
static int i = 0;
ENTRY * unused;
s[i].key = v1; s[i].i = v2, s[i].s=v3;.
e.key = v1; e.data=&s[i];
hsearch_r (e, ENTER, &unused, hash);
i++;
}
S * search (char * arg)
{
ENTRY e;
ENTRY * rv;
if (!arg)
return 0;
e.key = arg;
e.data = NULL;
hsearch_r (e, FIND, &rv, hash);
if (!rv)
return 0;
else
return rv->data;
}
int main (void)
{
int i;
hash = calloc(1, sizeof(struct hsearch_data));
hcreate_r(MAXP, hash);
add ("one", 1, "cat");
add ("two", 2, "dogs");
add ("three", 3, "cows");
for (i=(MAXP-1); i>=0; i--)
{
S * sea = search(s[i].key);
if (sea)
printf ("%s (%d) %s \n", sea->key, sea->i, sea->s);
}
}
Исправление lenin386, :
# define MAXP 10
# include <stdio.h>
# include <stdlib.h>
#define _GNU_SOURCE
#define __USE_GNU
#include <search.h>
typedef struct
{
char * key;
int i;
char * s;
} S;
S s[MAXP] = {0};
struct hsearch_data * hash;
void add (char * v1, int v2, char * v3)
{
ENTRY e;
static int i = 0;
ENTRY * unused;
s[i].key = v1; s[i].i = v2, s[i].s=v3;.
e.key = v1; e.data=&s[i];
hsearch_r (e, ENTER, &unused, hash);
i++;
}
S * search (char * arg)
{
ENTRY e;
ENTRY * rv;
if (!arg)
return 0;
e.key = arg;
e.data = NULL;
hsearch_r (e, FIND, &rv, hash);
if (!rv)
return 0;
else
return rv->data;
}
int main (void)
{
ENTRY e;
int i;
hash = calloc(1, sizeof(struct hsearch_data));
hcreate_r(MAXP, hash);
add ("one", 1, "cat");
add ("two", 2, "dogs");
add ("three", 3, "cows");
for (i=(MAXP-1); i>=0; i--)
{
S * sea = search(s[i].key);
if (sea)
printf ("%s (%d) %s \n", sea->key, sea->i, sea->s);
}
}
Исходная версия lenin386, :
# define MAXP 10
# include <stdio.h>
# include <stdlib.h>
#define _GNU_SOURCE
#define __USE_GNU
#include <search.h>
typedef struct
{
char * key;
int i;
char * s;
} S;
S s[MAXP] = {0};
struct hsearch_data * hash;
void add (char * v1, int v2, char * v3)
{
ENTRY e;
static int i = 0;
ENTRY * unused;
s[i].key = v1; s[i].i = v2, s[i].s=v3;.
e.key = v1; e.data=&s[i];
hsearch_r (e, ENTER, &unused, hash);
i++;
}
S * search (char * arg)
{
ENTRY e;
ENTRY * rv;
if (!arg)
return 0;
e.key = arg;
e.data = NULL;
hsearch_r (e, FIND, &rv, hash);
if (!rv)
return 0;
else
return rv->data;
}
int main (void)
{
ENTRY e;
int i;
hash = calloc(1, sizeof(struct hsearch_data));
hcreate_r(MAXP, hash);
add ("one", 1, "cat");
add ("two", 2, "dogs");
add ("three", 3, "cows");
for (i=(MAXP-1); i>=0; i--)
{
S * sea = search(s[i].key);
if (sea)
------> printf ("%s (%d) %s \n", sea->key, sea->i, sea->s);
}
}