L
lestrov1
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
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