将 int 转换为指针

converting int to pointer

本文关键字:指针 转换 int      更新时间:2023-10-16

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

有时将

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

C++您可以通过黑客来做到这一点;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;
}

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

但是,这是一个黑客;您不应该能够将int转换为指针,因为一般来说这是没有意义的。事实上,即使在 pthreads 的情况下,将指针传递给某个封装要传递的数据的结构也更合乎逻辑。

所以,基本上..."不要"。

你想这样做:

NumRecPrinted = &no_of_records;

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

然后打印它:

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

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

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

这是更正后的版本:

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

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

(int *)no_of_records为您提供指向地址no_of_records的指针。要获取指向 no_of_records 值的指针,您需要编写 &no_of_records

我真的很喜欢用联合来做这种事情:

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

了解指针和整数之间的关系很有用。它们都可以表示为数字并来回转换。让我们看以下示例:

int main()
{
    // pointer to int64_t
    int64_t* pointer = new int64_t(5);
    // an integer keeping address of the pointer in decimal format
    int64_t pointerAsNumber = (int64_t) pointer;
    // pointer to int64_t created from  int64_t integer
    int64_t* pointerFromNumber = (int64_t*)pointerAsNumber;
    // both print 5 - number stored in memory cell with address "pointer"
    cout << *pointer << endl;
    cout << *pointerFromNumber << endl;
    // both print same number - the address of memory cell that stores number 5
    // the printed numbers may differ between different program runs
    cout << pointer << endl;
    cout << pointerFromNumber << endl;
    // print address of the memory cell that holds 5 in decimal format
    cout << pointerAsNumber << endl;
    // print address of the memory cell that holds 5 in hexadecimal format
    // note, all 4 printed numbers, pointer, pointerFromNumber, pointerAsNumber and 
    //(hex) << pointerAsNumber are same numbers
    cout << (hex) << pointerAsNumber << endl;
    // now three DIFFERENT numbers will be printed
    // they hold addresses of pointer, pointerFromNumber and pointerAsNumber
    cout << &pointer << endl;
    cout << &pointerFromNumber << endl;
    cout << &pointerAsNumber << endl;
}

我发现将用户定义的数据类型与整数相互转换特别有用的可能性。这是另一个例子:

struct MyStruct {
    int a;
    int b;
    MyStruct(int a_, int b_) : a(a_), b(b_) {}
};
int main()
{
    MyStruct* ms = new MyStruct(1,2);
    // storing address of ms in msaddr ;
    uint64_t msaddr = (uint64_t)ms;
    // creating another MyStruct from the address
    MyStruct* ms2 = (MyStruct*)(msaddr);
   // the both MyStruct keep same numbers
    cout << ms->a << endl;
    cout << ms->b << endl;
    cout << ms2->a << endl;
    cout << ms2->b << endl;
   // but they are different structs in memory
    cout << &ms << endl;
    cout << &ms2 << endl;
}

请记住,不建议使用在整数和指针之间进行转换的演示可能性,并且应该非常谨慎地使用。