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

Discussion in 'C Programming' started by lestrov1@mail.ru, Oct 9, 2005.

  1. Guest

    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
    , Oct 9, 2005
    #1
    1. Advertising

  2. 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.
    Martin Ambuhl, Oct 9, 2005
    #2
    1. Advertising

  3. Guest

    thank you for answer!

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

    dmitriy
    , Oct 9, 2005
    #3
  4. 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.
    Martin Ambuhl, Oct 9, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonas

    glibc regular expression

    Jonas, Jul 14, 2003, in forum: C Programming
    Replies:
    2
    Views:
    562
    Jonas
    Jul 15, 2003
  2. =?UTF-8?B?0LTQsNC80ZjQsNC9INCzLg==?=

    access to glibc-2.3 threaded locale API

    =?UTF-8?B?0LTQsNC80ZjQsNC9INCzLg==?=, Sep 1, 2003, in forum: Python
    Replies:
    1
    Views:
    319
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Sep 1, 2003
  3. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  4. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    595
    David A. Black
    Jul 2, 2008
Loading...

Share This Page