Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question about hash in search.h (glibc, unix)

Reply
Thread Tools

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

 
 
lestrov1@mail.ru
Guest
Posts: n/a
 
      10-09-2005
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

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-09-2005
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.
 
Reply With Quote
 
 
 
 
lestrov1@mail.ru
Guest
Posts: n/a
 
      10-09-2005
thank you for answer!

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

dmitriy

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-09-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash of Hash of Arrays Question Älphä Blüë Ruby 5 07-18-2009 07:36 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57