2012-02-09 154 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