Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange Pointer behavior in while loop
    primarykey
    data
    text
    <p>Hello I insert some values into an hash table(the implemented hash table is well tested and works) I read in a file with fscanf and store the values of the string and the float in variables.</p> <p>The text file looks like this:</p> <pre><code>die 0.000000 laun -4.102468 des -7.952615 verliebt -6.080813 ein -8.645762 schaferspiel -4.315029 in -8.645762 vers -5.755391 und -8.645762 akt -3.909564 person -8.645762 egl -8.645762 </code></pre> <p>The read in works like this:</p> <pre><code>//scan text for scheme WORDSTEM PROBABLITIY r = fscanf(text, "%s %f\n", wordstem, &amp;prob); //doing some pointer madness!!! pProb = &amp;prob; float PpProb = *pProb; float *PPpProb = &amp;PpProb; char *cpStr = strdup(wordstem); hashtable_access(wordHash, cpStr,PPpProb); //insert into hashtable_t while (r != EOF){ float prob1; r = fscanf(text, "%s %f\n", wordstem, &amp;prob1); //printf("%f",prob1); char *cpStr1 = strdup(wordstem); float a = 0; float *PpProb1 = &amp;a; float *pProb1 = &amp;prob1; *PpProb1 = *pProb1; hashtable_access(wordHash, cpStr1,PpProb1); pProb1 = NULL; //do till EOF } float *pHashProb = (float *)hashtable_access(wordHash, "schaferspiel", NULL); float hashProb = *pHashProb; printf("%f\n",hashProb); </code></pre> <p>The problem is that the hash returns for the first entry "die" the correct value but schaferspiel and every other key after the first returns always the last value.</p> <p>The hash table requires to save pointers. I'm not sure what I'm doing wrong because i think I copied the value of prob1.</p> <p>Can somebody please help me. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload