C++ 将 int 转换为指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9212219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:29:46  来源:igfitidea点击:

converting int to pointer

c++pointers

提问by AJ.

I want to save intvalue to a pointer variable. But I get an error:

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

I tried doing this but I get 0 as return:

我尝试这样做,但我得到 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 returns as 0

NumRecPrinted 返回 0

采纳答案by mattjgalloway

You want to be doing this:

你想这样做:

NumRecPrinted = &no_of_records;

i.e. you're taking the address of no_of_recordsand assigning it to NumRecPrinted.

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

And then to print it:

然后打印它:

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

i.e. you're dereferencing NumRecPrintedwhich will get the intstored at the memory address pointed to by NumRecPrinted.

即您正在取消引用NumRecPrinted,它将int存储在指向的内存地址处NumRecPrinted

回答by Lightness Races in Orbit

It's sometimes useful to "encode" a non-pointer value into a pointer, for instance when you need to pass data into a pthreadsthread argument (void*).

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

In C++ you can do this by hackery; C-style casts are an example of this hackery, and in fact your program works as desired:

在 C++ 中,你可以通过hackery 来做到这一点;C 风格的强制转换是这种技巧的一个例子,实际上你的程序按预期工作

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

You just need to realise that 0xais a hexadecimal representation of the decimal 10.

您只需要意识到这0xa是十进制的十六进制表示10

However, this isa hack; you're not supposed to be able to convert ints to pointers because in generalit makes no sense. In fact, even in the pthreadscase it's far more logical to pass a pointer to some structure that encapsulates the data you want to pass over.

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

So, basically... "don't".

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

回答by Dzek Trek

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

回答by Abrixas2

(int *)no_of_recordsgives you a pointer to the address no_of_records. To get a pointer to the value of no_of_records, you need to write &no_of_records.

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

回答by NPE

Here is the corrected version:

这是更正后的版本:

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

Note the added ampersand and the asterisk.

请注意添加的与号和星号。

回答by Gigi

I really like using union for this sort of stuff:

我真的很喜欢将 union 用于此类内容:

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