Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting value from pointer
    primarykey
    data
    text
    <p>I'm having problem getting the value from a pointer. I have the following code in C++: </p> <pre><code>void* Nodo::readArray(VarHash&amp; var, string varName, int posicion, float&amp; d) { //some code before... void* res; float num = bit.getFloatFromArray(arregloTemp); //THIS FUNCTION RETURN A FLOAT AND IT'S OK cout &lt;&lt; "NUMBER " &lt;&lt; num &lt;&lt; endl; d = num; res = &amp;num; return res } int main() { float d = 0.0; void* res = n.readArray(v, "c", 0, d); //THE VALUES OF THE ARRAY ARE: {65.5, 66.5}; float* car3 = (float*)res; cout &lt;&lt; "RESULT_READARRAY " &lt;&lt; *car3 &lt;&lt; endl; cout &lt;&lt; "FLOAT REFERENCE: " &lt;&lt; d &lt;&lt; endl; } </code></pre> <p>The result of running this code is the following: </p> <blockquote> <p>NUMBER 65.5 RESULT_READARRAY -1.2001<br> //INCORRECT IT SHOULD BE LIKE NUMBER<br> FLOAT REFERENCE: 65.5 //CORRECT </p> <p>NUMBER 66.5 RESULT_READARRAY -1.2001<br> //INCORRECT IT SHOULD BE LIKE NUMBER<br> FLOAT REFERENCE: 66.5 //CORRECT </p> </blockquote> <p>For some reason, when I get the value of the pointer returned by the function called readArray is incorrect. I'm passing a float variable(d) as a reference in the same function just to verify that the value is ok, and as you can see, THE FLOAT REFERENCE matches the NUMBER. If I declare the variable num(read array) as a static float, the first RESULT_READARRAY will be 65.5, that is correct, however, the next value will be the same instead of 66.5. Let me show you the result of running the code using static float variable: </p> <blockquote> <p>NUMBER 65.5 RESULT_READARRAY 65.5<br> //PERFECT FLOAT REFERENCE: 65.5<br> //¨PERFECT </p> <p>NUMBER 65.5 //THIS IS INCORRECT, IT<br> SHOULD BE 66.5 RESULT_READARRAY 65.5<br> FLOAT REFERENCE: 65.5 </p> </blockquote> <p>Do you know how can I get the correct value returned by the function called readArray()?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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