Find the answer to your Linux question:
Results 1 to 2 of 2
Hello all!! How I can return hash-data from function into main(): Code: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <search.h> void exists&#40;char *k&#41;&#123; //add values into a hash ENTRY e,*ep; ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2005
    Posts
    1

    why dont work?


    Hello all!!

    How I can return hash-data from function into main():
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <search.h>
    
    void exists&#40;char *k&#41;&#123;
    //add values into a hash
      ENTRY e,*ep; int c=1;
      e.key = k;
      ep = hsearch&#40;e, FIND&#41;;
      printf&#40;"%9.9s -> %9.9s&#58;%d \n", e.key,
        ep ? ep->key &#58; "NULL",
        ep ? &#40;int&#41;&#40;ep->data&#41; &#58; 0&#41;;
      e.key=k;
      e.data = &#40;char *&#41;c;
      ep = hsearch&#40;e, ENTER&#41;;
      if &#40;ep == NULL&#41; &#123;
        fprintf&#40;stderr, "entry failed\n"&#41;;
        exit&#40;1&#41;;
      &#125;
    
    &#125;
    
    void rec&#40;&#41;&#123;
    //call exists&#40;&#41;
      char num&#91;3&#93;;
      int i,j;
      for &#40;i = 2; i <= 3; i++&#41; &#123;
        for &#40;j = 2; j <= 3; j++&#41; &#123;
          sprintf&#40;num,"%d %d",i,j&#41;;
          exists&#40;num&#41;;
        &#125;
      &#125;
    
    &#125;
    
    int main&#40;&#41; &#123;
      ENTRY e,*ep; char nums&#91;3&#93;;
      int ii=8;
      int i,j;
      hcreate&#40;ii&#41;;
      i=2;j=2;
      rec&#40;&#41;;
    // show data from hash
      for &#40;i = 2; i <= 3; i++&#41; &#123;
        for &#40;j = 2; j <= 3; j++&#41; &#123;
          sprintf&#40;nums,"%d %d",i,j&#41;;
          e.key = nums;
          ep = hsearch&#40;e, FIND&#41;;
          printf&#40;"&#91;%9.9s -> %9.9s&#58;%d &#93;\n", e.key,
            ep ? ep->key &#58; "NULL",
            ep ? &#40;int&#41;&#40;ep->data&#41; &#58; 0&#41;;
        &#125;
      &#125;
      return 1;
    
    &#125;
    
    $ gcc test.c;./a.out
          2 2 ->      NULL&#58;0
          2 3 ->      NULL&#58;0
          3 2 ->      NULL&#58;0
          3 3 ->      NULL&#58;0
    &#91;      2 2 ->      NULL&#58;0 &#93;
    &#91;      2 3 ->      NULL&#58;0 &#93;
    &#91;      3 2 ->      NULL&#58;0 &#93;
    &#91;      3 3 ->      NULL&#58;0 &#93;
    $
    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. i'm beginner in C, but life...

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <search.h>
    
    ENTRY e,*ep;
    void get&#40;&#41;
    &#123;
            char nums&#91;3&#93;;
            int i,j;
    
            for &#40;i = 2; i <= 3; i++&#41;
            &#123;
                for &#40;j = 2; j <= 3; j++&#41;
                &#123;
                  sprintf&#40;nums,"%d%d",i,j&#41;;
                  e.key = nums;
                  ep = hsearch&#40;e, FIND&#41;;
                  printf&#40;"&#91;%9.9s -> %9.9s&#58;%d &#93;\n", e.key, ep ? ep->key &#58; "NULL", ep ? &#40;int*&#41;&#40;ep->data&#41; &#58; 0&#41;;
                &#125;
            &#125;
    &#125;
    
    
    void exists&#40;char *k&#41;
    &#123;
    //add values into a hash
    
              ENTRY e,*ep; int c=1;
            int i,j;
            char nums&#91;3&#93;;
            for &#40;i = 2; i <= 3; i++&#41;
            &#123;
                    for &#40;j = 2; j <= 3; j++&#41;
                    &#123;
                            // this pointer should be freed later on during exit
                            e.key=&#40;char*&#41;malloc&#40;5&#41;;
                            sprintf&#40;nums,"%d%d",i,j&#41;;
                            strcpy&#40;e.key,nums&#41;;
                            // this pointer should be freed later on during exit
                            e.data=&#40;char*&#41;malloc&#40;5&#41;;
                            e.data=&#40;int*&#41;c;
    
                            ep = hsearch&#40;e, ENTER&#41;;
                            if &#40;ep == NULL&#41;
                            &#123;
                                fprintf&#40;stderr, "entry failed\n"&#41;;
                                hdestroy&#40;&#41;;
                                exit&#40;1&#41;;
                            &#125;
                    &#125;
            &#125;
    &#125;
    
    void rec&#40;&#41;
    &#123;
      char num&#91;3&#93;;
      int i,j;
      for &#40;i = 2; i <= 3; i++&#41;
      &#123;
        for &#40;j = 2; j <= 3; j++&#41;
        &#123;
          sprintf&#40;num,"%d%d",i,j&#41;;
          exists&#40;num&#41;;
        &#125;
      &#125;
    &#125;
    
    int main&#40;&#41;
    &#123;
            ENTRY e,*ep;
            char nums&#91;3&#93;;
            int ii=8,c=1;
            int i,j;
    
            //creating the hash table
            hcreate&#40;ii&#41;;
            //entering data into the hash table
            rec&#40;&#41;;
            // show data from hash
            get&#40;&#41;;
            //destroys the memory allocated for the table
            //but does not destroy the memory allocated for the key and data
            // that has to be freed by the user
            hdestroy&#40;&#41;;
    
            return 1;
    &#125;
    It did not work since you have to allocate memory for the key and the data of the structure ENTRY....

    BE careful there is a memory leak in this program... as i have not freed the pointers which i have to free when the program is over

    hdestroy does not free all the pointers.
    It is the users duty to free thos pointers... so u will have to store those pointers as another list and then free them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •