2012-02-09 191 views
3

我想將int值保存到指針變量中。但是,我得到一個錯誤:將int轉換爲指針

#include <iostream> 
using namespace std; 

int main() 
{ 
    int *NumRecPrinted = NULL; 
    int no_of_records = 10; 
    NumRecPrinted = (int*)no_of_records; // <<< Doesn't give value of NumRecPrinted 

    cout << "NumRecPrinted!" << NumRecPrinted; 
    return 0; 
} 

我試着這樣做,但我得到0作爲回報:

int main() 
{ 
    int demo(int *NumRecPrinted); 
    int num = 2; 
    demo(&num); 
    cout << "NumRecPrinted=" << num; <<<< Prints 0 
    return 0; 
} 

int demo (int *NumRecPrinted) 

{ 
    int no_of_records = 11; 
    NumRecPrinted = &no_of_records; 
} 

NumRecPrinted返回0

+1

你從來沒有告訴我們是什麼錯誤。 – 2012-02-09 14:08:58

+0

你爲什麼要這麼做?將整數轉換爲指針的原因很少,所以我想知道你正在嘗試做什麼可能不會以其他方式更好地實現。或者你的意思是*獲得一個指向一個整數的指針?* – 2012-02-09 14:21:23

+0

我正在做一個需要返回指針的函數。 – 2012-02-09 14:24:47

回答

2

你想要做的:

NumRecPrinted = &no_of_records; 

即你正在服用的no_of_records地址並將其分配給NumRecPrinted

然後將其打印出來:

cout << "NumRecPrinted!" << *NumRecPrinted; 

即您提領NumRecPrinted將得到int存儲在內存地址指向NumRecPrinted

+2

不,我認爲他試圖將'10'編碼成指針。 – 2012-02-09 14:08:30

-1

這裏是修正版本:

#include <iostream> 
using namespace std; 
int main() 
{ 
    int *NumRecPrinted = NULL; 
    int no_of_records = 10; 
    NumRecPrinted = &no_of_records; // take the address of no_of_records 

    cout << "NumRecPrinted!" << *NumRecPrinted; // dereference the pointer 
    return 0; 
} 

請注意添加的&符號和星號。

+0

不,我認爲他正試圖將'10'編碼成指針。 – 2012-02-09 14:11:52

0

(int *)no_of_records給你一個指向地址no_of_records的指針。要獲得指向no_of_records的值的指針,您需要編寫&no_of_records

+0

不,我認爲他試圖將'10'編碼成指針。 – 2012-02-09 14:12:07

4

它有時爲「編碼」非指針值成有用的指針,例如當您需要將數據傳遞到並行線程螺紋參數(void*)。

在C++中,你可以通過hackery來做到這一點; C風格的轉換都是這兩輪牛車的一個例子,實際上your program works as desired

#include <iostream> 
using namespace std; 

int main() 
{ 
    int *NumRecPrinted = NULL; 
    int no_of_records = 10; 
    NumRecPrinted = (int*)no_of_records; 

    cout << "NumRecPrinted!" << NumRecPrinted; // Output: 0xa (same as 10) 
    return 0; 
} 

你只需要認識到,0xa是小數10的十六進制表示。

但是,這個的破解;你不應該能夠將int s轉換成指針,因爲在通用這是沒有意義的。事實上,即使在的情況下,將指針傳遞給封裝你想要傳遞的數據的某個結構也是合乎邏輯的。

所以,基本上......「不要」。

+1

但是,如果您必須...使用[intptr_t](http://stackoverflow.com/questions/6326338/why-when-to-use-intptr-t-for-type-casting-in-c) – Useless 2012-02-09 14:24:50

+1

.. 。或者至少static_assert(sizeof(int)> = sizeof(void *),「int的大小必須大於或等於指針的大小」);',即記錄你的假設。 – 2012-02-09 14:34:23

+0

你們兩個都是對的。 – 2012-02-09 15:10:45

0

我真的很喜歡使用聯盟這種東西:

#include <iostream> 
using namespace std; 

int main() 
{ 
    static_assert(sizeof(int) == sizeof(int*)); 

    union { int i; int* p; } u { 10 }; 

    cout << "NumRecPrinted! " << u.p; 
    return 0; 
} 
0
#include <iostream> 
using namespace std; 

int main() 
{ 
int *NumRecPrinted = NULL; // assign pointer NumRecPrinted to be valued as NULL 
int *NumRecPrinted2 = NULL; 
int no_of_records = 10; // initialize the value of the identificator no_of_records 
NumRecPrinted = (int*)no_of_records; // sets a pointer to the address no_of_records 
NumRecPrinted2 = &no_of_records; // gives a pointer to the value of no_of_records 

cout << "NumRecPrinted!" << NumRecPrinted; // address of no_of_records 0000000A 
cout << "NumRecPrinted!" << *NumRecPrinted2; // value of no_of_records 10 
system("pause"); // ninja 
return 0; 
} 
+1

爲什麼沒有壓痕?你爲什麼添加'system(「pause」)'?這樣做會讓寶寶耶穌哭泣。 – 2013-01-28 13:24:08