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. Advertisements

  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. Advertisements

  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. Advertisements

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. Replies:
    12
    Views:
    1,924
    Dave Thompson
    Jan 10, 2005
  2. Replies:
    18
    Views:
    857
    Dave Thompson
    Jan 10, 2005
  3. Abby Lee
    Replies:
    5
    Views:
    694
    Abby Lee
    Aug 2, 2004
  4. rp
    Replies:
    1
    Views:
    853
    red floyd
    Nov 10, 2011
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    293
    Ronald Fischer
    Aug 23, 2007
  6. Srijayanth Sridhar
    Replies:
    19
    Views:
    991
    David A. Black
    Jul 2, 2008
  7. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    892
    Älphä Blüë
    Jul 18, 2009
  8. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    563
    Michele Dondi
    Jan 22, 2004
Loading...