将 int 转换为指针
converting int to pointer
我想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;
}
请记住,不建议使用在整数和指针之间进行转换的演示可能性,并且应该非常谨慎地使用。
相关文章:
- 正在将指针转换为范围
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- C++:Lambda 函数指针转换的用例是什么?
- 如何将 int 指针转换为浮点指针
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 将(N 个字节)无符号字符指针转换为浮点数和双 C++
- 我们可以在不知道其真实类型的情况下将基类指针转换为派生类指针吗?
- C++中数组大小未知的指针转换
- Antlr cpp 运行时 任何错误的指针转换?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- C 指针转换会导致内存访问冲突
- 模板类实例化中的指针转换无效
- 函数调用歧义(用户定义的转换和 Derived2Base 指针转换)
- 如何将 void(*)() 类型的指针转换为 void*
- 数组到指针转换期间的临时具体化
- 直接为浮点变量分配十六进制整数与通过指针转换分配之间的区别
- 将基类指针转换为派生的类指针
- 应对 std::字符串中的 std::<char>指针转换后的向量
- 在编译时将函数指针转换为 std::uintptr_t
- 将基类指针转换为未知派生类指针