Don't understand how tsearch return pointers work

I am trying to understand how the tsearch library works, and I have reached the point where I am completely at a dead end. Here is my code:

#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <dirent.h>
#include <utime.h>
#include <sys/wait.h>
#include <sys/msg.h>
#include <signal.h>
#include <ctype.h>
#include <search.h>

int compare (const void *a, const void *b);
void action(const void *nodep, VISIT value, int level);

struct word {
    char word[100];
    int occur;          
};

int main (void) {
    void *root = NULL;
    char *words[] = {"a","b","c","a"};
    struct word *entry = malloc(10 * sizeof(struct word));  
    struct word *ptr;
    struct word *ptr2;
    int i;  

    for (i=0;i<4;i++) {
        memcpy(entry[i].word,words[i],100);
        entry[i].occur = 1;                 
        ptr = tfind(&entry[i],&root,compare);
        if (ptr == NULL) {          
            tsearch(&entry[i],&root,compare);
        }
        else {              
            printf("%i\n",ptr->occur);
            printf("%i\n",entry[0].occur);          
            entry[0].occur++;           
        }
    }
    twalk (root, action);   
    //tdestroy (&rootp,freefct);
    return 0;
}

int compare (const void *a, const void *b) {
    const struct word *w1, *w2;

    w1 = (const struct word *) a;
    w2 = (const struct word *) b;

    return strcmp(w1->word, w2->word);
}

void action(const void *nodep, VISIT value, int level) {
    struct word *w = *((struct word **) nodep);
    switch (value) {
    case leaf:
    case postorder:
        printf("%s: %i\n",w->word, w->occur);
        break;
    default:
        break;
    }
    return;
}

Now I think that ptr should point to the record [0] (because that is where the value found) and ptr-> arise to give me a value for "a". But this is not so. This gives me 0, as shown in the results below:

0 1 a: 2 b: 1 c: 1

Changing the value of the entry [0] changes what it prints while walking.

I tried every ptr dereference or casting combination that I can think of by declaring it as void * or as struct word * ...

So my question is basically this:

Given the return from tfind (and what should be declared as), how do I access the values ​​inside the structure?

+3
1

tfind , node, . node , ptr **.

    else {
        printf("i=%d ptr=%p *ptr=%p entry=%p,%p,%p,%p\n", i, ptr, *(struct word **)ptr, entry, entry+1, entry+2, entry+3);
        printf("wrong: %i\n",ptr->occur);
        printf("right: %i\n",(*(struct word **)ptr)->occur);
        printf("right: %i\n",entry[0].occur);
        entry[0].occur++;
    }

( , )

i=3 ptr=0x8f32430 *ptr=0x8f32010 entry=0x8f32010,0x8f32078,0x8f320e0,0x8f32148
wrong: 0
right: 1
right: 1
a: 2
b: 1
c: 1

, ptr, elem [0], * ptr.

+3

All Articles