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(char *k){
//add values into a hash
ENTRY e,*ep; ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-09-2005 #1Just Joined!
- Join Date
- Oct 2005
- Posts
- 1
why dont work?
Hello all!!
How I can return hash-data from function into main():
Where I have made a mistake? If not a mistake, as it is necessary toCode:#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 ] $
make to see it data?
thank you for help!
dmitriyk kuvshinov aka vilfred
p.s. i'm beginner in C, but life...
- 10-14-2005 #2Linux Newbie
- Join Date
- May 2005
- Location
- Chennai,TamilNadu, India
- Posts
- 141
It did not work since you have to allocate memory for the key and the data of the structure ENTRY....Code:#include <stdlib.h> #include <stdio.h> #include <string.h> #include <search.h> ENTRY e,*ep; void get() { char nums[3]; int i,j; 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); } } } void exists(char *k) { //add values into a hash ENTRY e,*ep; int c=1; int i,j; char nums[3]; for (i = 2; i <= 3; i++) { for (j = 2; j <= 3; j++) { // this pointer should be freed later on during exit e.key=(char*)malloc(5); sprintf(nums,"%d%d",i,j); strcpy(e.key,nums); // this pointer should be freed later on during exit e.data=(char*)malloc(5); e.data=(int*)c; ep = hsearch(e, ENTER); if (ep == NULL) { fprintf(stderr, "entry failed\n"); hdestroy(); exit(1); } } } } void rec() { 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,c=1; int i,j; //creating the hash table hcreate(ii); //entering data into the hash table rec(); // show data from hash get(); //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(); return 1; }
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


Reply With Quote
