question about hash in search.h (glibc, unix)

L

lestrov1

Hello all!!

How I can return data from a hash:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <search.h>

void exists(char *k){
//add values into a hash
ENTRY e,*ep; int c=1;
e.key = k;
ep = hsearch(e, FIND);
printf("%9.9s -> %9.9s:%d \n", e.key,
ep ? ep->key : "NULL",
ep ? (int)(ep->data) : 0);
e.key=k;
e.data = (char *)c;
ep = hsearch(e, ENTER);
if (ep == NULL) {
fprintf(stderr, "entry failed\n");
exit(1);
}

}

void rec(){
//call exists()
char num[3];
int i,j;
for (i = 2; i <= 3; i++) {
for (j = 2; j <= 3; j++) {
sprintf(num,"%d %d",i,j);
exists(num);
}
}

}

int main() {
ENTRY e,*ep; char nums[3];
int ii=8;
int i,j;
hcreate(ii);
i=2;j=2;
rec();
// show data from hash
for (i = 2; i <= 3; i++) {
for (j = 2; j <= 3; j++) {
sprintf(nums,"%d %d",i,j);
e.key = nums;
ep = hsearch(e, FIND);
printf("[%9.9s -> %9.9s:%d ]\n", e.key,
ep ? ep->key : "NULL",
ep ? (int)(ep->data) : 0);
}
}
return 1;

}

$ gcc test.c;./a.out
2 2 -> NULL:0
2 3 -> NULL:0
3 2 -> NULL:0
3 3 -> NULL:0
[ 2 2 -> NULL:0 ]
[ 2 3 -> NULL:0 ]
[ 3 2 -> NULL:0 ]
[ 3 3 -> NULL:0 ]
$

Where I have made a mistake? If not a mistake, as it is necessary to
make to see it data?

thank you for help!

dmitriyk kuvshinov aka vilfred

p.s. sorry for crosspost into gnu.glibc.bug
 
M

Martin Ambuhl

Hello all!!

How I can return data from a hash:
To even compile your code, I had to change everything before the
definition of exists() to

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Everything through 'end of non-standard additions' is non-standard
and may be very different on your implementation, both in form and
function */

typedef struct
{
char *key;
void *data;
} ENTRY; /* non-standard type added */

typedef enum
{ FIND, ENTER } ACTION; /* non-standard enum added */

ENTRY *hsearch(ENTRY, ACTION); /* prototype for non-standard function
added */

int hcreate(size_t); /* prototype for non-standard function
added */

/* end of non-standard additions */


But, of course, it still doesn't link. Please take your question to a
place where it is topical. Perhaps a linux group will do you.
 
L

lestrov1

thank you for answer!

i'm sorry for posting my message in windows specific gorup...

dmitriy
 
M

Martin Ambuhl

thank you for answer!

i'm sorry for posting my message in windows specific gorup...

comp.lang.c is not a windows-specific group. Your code was
*nix-specific. But C is not tied to a platform. All platform-specific
questions are off-topic. We are much more often accused of being
anti-windows and pro-*nix than being windows-specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top