【问题标题】:converting int to pointer将 int 转换为指针
【发布时间】:2012-03-02 00:05:25
【问题描述】:

我想将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

【问题讨论】:

  • 你从来没有告诉我们错误是什么。
  • 您为什么要这样做?将整数转换为指针的充分理由很少,所以我想知道您正在尝试做的事情是否可能无法以其他方式更好地实现。还是您的意思是获取指向整数的指针?
  • 我在一个需要返回指针的函数中执行此操作。
  • 在您的demo 函数中,您修改NumRecPrinted 以指向一个局部变量...它在demo 之外没有用...
  • @AJ。我希望你没有计划a hotel heist

标签: c++ pointers


【解决方案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;
}

注意添加的 & 和星号。

【讨论】:

    【解决方案2】:

    你想这样做:

    NumRecPrinted = &no_of_records;
    

    即您正在获取no_of_records 的地址并将其分配给NumRecPrinted

    然后打印出来:

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

    即您正在取消引用NumRecPrinted,这将使int 存储在NumRecPrinted 指向的内存地址中。

    【讨论】:

      【解决方案3】:

      (int *)no_of_records 给你一个指向地址no_of_records 的指针。要获得指向no_of_records 值的指针,您需要编写&amp;no_of_records

      【讨论】:

        【解决方案4】:

        有时将非指针值“编码”为指针很有用,例如当您需要将数据传递到 pthreads 线程参数 (void*) 时。

        在 C++ 中,你可以通过骇客来做到这一点; 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 的十六进制表示。

        然而,这 hack;您不应该能够将 ints 转换为指针,因为在 general 中它没有任何意义。事实上,即使在 pthreads 的情况下,将指针传递给封装了您要传递的数据的某个结构也更合乎逻辑。

        所以,基本上......“不要”。

        【讨论】:

        • 但是如果你必须...使用intptr_t
        • ...或至少static_assert(sizeof(int) &gt;= sizeof(void*), "the size of int must be greater than or equal to the size of a pointer");,即记录您的假设。
        【解决方案5】:

        我真的很喜欢在这类事情上使用联合:

        #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;
        }
        

        【讨论】:

        • 这是 C++ 中未定义的行为。
        【解决方案6】:
        #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;
        }
        

        【讨论】:

        • 为什么没有缩进?为什么要添加system("pause")?这样做会让婴儿耶稣哭泣。
        猜你喜欢
        • 2017-02-12
        • 2014-05-02
        • 1970-01-01
        • 2013-11-18
        • 2017-07-25
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多