【问题标题】:Null pointer Exception ( int Vs char) [duplicate]空指针异常(int Vs char)[重复]
【发布时间】:2016-05-13 04:31:37
【问题描述】:

方案一:

#include <iostream>
using namespace std;
int main ()
{
   int  *ptr = NULL;
   cout << "The value of ptr is " << ptr ;
   return 0;
}

方案二:

#include <iostream>
using namespace std;
int main ()
{
   char  *ptr = NULL;
   cout << "The value of ptr is " << ptr ;
   return 0;
}

问题是,在程序 1 中,输出稳定为:

  The value of ptr is 00000000

而在程序 2 中,我使用 char 指针而不是 int 指针,它给了我一个异常。

谁能帮我理解一下?

【问题讨论】:

    标签: c++ pointers


    【解决方案1】:

    因为

    【讨论】:

    • 我可以避免在 char 指针的情况下取消引用吗?
    • @MukeshPareek 见this duplicate。
    • 您可以将 char* 转换为 int,例如:(int)ptr
    • @WhozCraig 谢谢你的链接
    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 2014-07-28
    • 2016-04-28
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多