c - Don't understand how tsearch return pointers work -


I am trying to figure out how the library works and I have reached a point where I I'm stumped by the way. Here is my code:

  #include & lt; Stdio.h & gt; # Include & lt; Getopt.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Sys / stat.h & gt; # Include & lt; Fcntl.h & gt; # Include & lt; Unistd.h & gt; #include & lt; String.h & gt; # Include & lt; Errno.h & gt; # Include & lt; Time.h> # Include & lt; Stdlib.h & gt; # Include & lt; Dirent.h & gt; # Include & lt; Utime.h> # Include & lt; Sys / wait.h & gt; # Include & lt; Sys / msg.h> # Include & lt; Signal.h & gt; # Include & lt; Ctype.h & gt; # Include & lt; Search.h & gt; Compare Int (Constant Worth * A, Cost Blank * B); Zero action (constant view * nodep, visit value, int level); Structure word {four words [100]; Integer; }; Int main (zero) {zero * root = zero; Four * words [] = {"one", "b", "c", "a"}; Structure word * entry = molk (10 * size (word structure)); Structure word * ptr; Structure word * ptr2; Int i; For (i = 0; i <4; i ++) {memcpy (entry [i]. Words, word [i], 100); Entry [i] .occur = 1; Compare Ptr = tfind (& amp; entry [i], & amp; root;); If (ptr == NULL) {tsearch (& amp; entry [i], & amp; route, compare); } Other {printf ("% i \ n", ptr-> Yes); Printf ("% i \ n", entry [0] .occur); Entry [0] .occur ++; }} Twalk (root, action); // tdestroy (& rootp, freefact); Return 0; } Compare Int (Cont Woed * A, Constwid * B) {Construct word word * w1, * w2; W1 = (contract word * a) a; W2 = (const struct word *) b; Return strcmp (w1- & gt; word, w2- & gt;); } Zero Action (Constant Worth * Nodp, Visit Value, Int Level) {Structure word * w = * ((struct word **) nodep); Switch (value) {case leaf: case post order: printf ("% s:% i \ n", w-> word, w-> occurs); break; Default: Break; } Return; }  

Now, I would think that the PTR will indicate entry [0] (because it is where it has been found) and PTR-> me "A" But this is not it gives me 0, as shown in the results below:

0 1 a: 2 b: 1 c: 1

Change the value of the entry [ 0] changes which it prints during running.

I have tried every combination of defrferencing or casting ptr, I can declare it as zero * or as the struct word * ... ...

So my question is basically:

Looking at the return from tfind (and in what form should it be declared), how do I get the values ​​within the structure?

Return value from tefind is not an entry, it is an indicator of tree-internal node structure, the first of which Entry entry is an indicator. As the node structure is opaque, you can think of PTR as a structure word **.

If you write your second part again

  else {printf ("i =% d ptr =% p * ptr =% p entry =% p,% p ,% P,% p \ n ", i, ptr, * (structure term **) ptr, entry, entry + 1, entry + 2, entry + 3); Printf ("wrong:% i \ n", ptr->;); Printf ("true:% i \ n", (* (word of structure **) ptr) -> occurs; Printf ("correct:% i \ n", entry [0] .cokur); entry [ 0] .occur ++;}  

You (at least on my machine)

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

As you see, this is not a PTR The AMM shows [0], but * is PTR.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -