Jep*_*ppe 1 c++ pointers char memory-address
char testChar = 'a';
char myCharString[] = "asd";
char *pointerToFirstChar = &(myCharString[0]);
char *pointerToSecondChar = &(myCharString[1]);
cout << "A char takes " << sizeof(testChar) << " byte(s)";
cout << "Value was " << pointerToFirstChar << ", address: " << &pointerToFirstChar << endl;
cout << "Value 2 was " << pointerToSecondChar << ", address:" << &pointerToSecondChar << endl;
Run Code Online (Sandbox Code Playgroud)
这个输出:
"一个字符需要1个字节"
"......地址:00F3F718"
"......地址:00F3F70C",
我认为地址之间的区别应该是1个字节,因为这将是分隔它们的数据的大小.为什么不是这样?
&pointerToFirstChar并且&pointerToSecondChar,你不走的元素的地址char数组,但局部变量的地址pointerToFirstChar和pointerToSecondChar.请注意他们自己一直在指点.
你可能想要:
cout << "Value was " << pointerToFirstChar << ", address: " << static_cast<void*>(pointerToFirstChar) << endl;
cout << "Value 2 was " << pointerToSecondChar << ", address:" << static_cast<void*>(pointerToSecondChar) << endl;
Run Code Online (Sandbox Code Playgroud)
请注意,您需要将它们转换void*为打印地址而不是字符串.