我正在制作一个从C ++开始的测试程序:)
第一次打印后显示错误的值
这是代码(非常简单)
#include "pch.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
int varInt = 123456;
char varString[] = "DefaultString";
char arrChar[128] = "Long char array right there ->";
int * ptr2int;
ptr2int = &varInt;
int ** ptr2ptr;
ptr2ptr = &ptr2int;
int *** ptr2ptr2;
ptr2ptr2 = &ptr2ptr;
while(1){
cout << "Process ID: " << GetCurrentProcessId() << endl;
cout << "varInt (0x" << &varInt << ") = " << varInt << endl;
cout << "varString (0x" << &varString << ") = " << varString << endl;
cout << "varChar (0x" << &arrChar << ") = " << arrChar << endl;
cout << "ptr2int (0x" << hex << &ptr2int << ") = " << ptr2int << endl;
cout << "ptr2ptr (0x" << hex << &ptr2ptr << ") = " << ptr2ptr << endl;
cout << "ptr2ptr2 (0x" << hex << &ptr2ptr2 << ") = " << ptr2ptr2 << endl;
cout << "Press ENTER to print again." << endl;
getchar();
cout << "-----------------------------------" << endl;
}
return 0;
}
由于代码按原样发布,因此预期结果显而易见:
Process ID
是整数,因此应返回12704
(或任何int值)而不是31a0
varInt
也是整数,应该返回123456
而不是1e240
答案 0 :(得分:3)
1e240与123456相同(十六进制)。第一次迭代将正确打印123456,但是将cout的基本标志设置为十六进制模式后,需要将其设置回dec才能在下一个循环中再次打印123456。
cout << "varInt (0x" << &varInt << ") = " << dec << varInt << endl;
有关文档,请参见here。