![]() |
question about hash in search.h (glibc, unix)
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 |
Re: question about hash in search.h (glibc, unix)
lestrov1@mail.ru wrote:
> 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. |
Re: question about hash in search.h (glibc, unix)
thank you for answer!
i'm sorry for posting my message in windows specific gorup... dmitriy |
Re: question about hash in search.h (glibc, unix)
lestrov1@mail.ru wrote:
> 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. |
| All times are GMT. The time now is 06:00 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.