我有問題從指針獲取值。我在C++下面的代碼:從指針獲得價值
void* Nodo::readArray(VarHash& var, string varName, int posicion, float& d)
{
//some code before...
void* res;
float num = bit.getFloatFromArray(arregloTemp); //THIS FUNCTION RETURN A FLOAT AND IT'S OK
cout << "NUMBER " << num << endl;
d = num;
res = #
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 << "RESULT_READARRAY " << *car3 << endl;
cout << "FLOAT REFERENCE: " << d << endl;
}
運行這段代碼的結果是這樣的:
NUMBER 65.5 RESULT_READARRAY -1.2001
//不正確應該像NUMBER
FLOAT參考:65.5 //正確NUMBER 66.5 RESULT_READARRAY -1.2001
//不正確應該像NUMBER
FLOAT REFERENCE:66.5 // CORRECT
由於某些原因,當我得到由名爲readArray的函數返回的指針值不正確時。我在同一個函數中傳遞一個浮點變量(d)作爲參考,以驗證該值是否正確,正如您所見,FLOAT REFERENCE與NUMBER匹配。如果我將變量num(讀數組)聲明爲靜態浮點數,則第一個RESULT_READARRAY將是65.5,這是正確的,但是,下一個值將是相同的,而不是66.5。讓我告訴你使用靜態浮動變量運行代碼的結果:
NUMBER 65.5 RESULT_READARRAY 65.5
// PERFECT FLOAT參考:65.5
//完美NUMBER 65.5 //這是錯誤的時候,
應該是66.5 RESULT_READARRAY 65.5
FLOAT參考:65.5
你知道我怎麼能GE t由readArray()函數返回的正確值?
+1大的解決方法,即是我在找的東西:D 感謝馬克 – Eric 2010-05-04 16:46:42