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;
this outputs:
这个输出:
"A char takes 1 byte"
" char取1个字节"
"... address: 00F3F718"
“…地址:00 f3f718”
"... address: 00F3F70C",
“…地址:00 f3f70c”,
I'm thinking the difference between addresses should be 1 byte, since that would be the size of the data separating them. Why is it not so?
我认为地址之间的区别应该是1个字节,因为这是数据分离的大小。为什么不是这样呢?
3 个解决方案
#1
5
&pointerToFirstChar
and &pointerToSecondChar
, you're not taking the address of the elements of the char
array, but the address of the local variable pointerToFirstChar
and pointerToSecondChar
. Note they've been pointers themselves.
&pointerToFirstChar和&pointerToSecondChar,您没有获取char数组元素的地址,而是本地变量pointerToFirstChar和pointerToSecondChar的地址。注意,它们都是指针。
You might want:
你可能会想:
cout << "Value was " << pointerToFirstChar << ", address: " << static_cast<void*>(pointerToFirstChar) << endl;
cout << "Value 2 was " << pointerToSecondChar << ", address:" << static_cast<void*>(pointerToSecondChar) << endl;
Note you need to cast them to void*
to print out the address instead of the string.
注意,您需要将它们转换为void*以打印地址而不是字符串。
#2
0
You're looking at the addresses of the pointers pointerToFirstChar
and pointerToSecondChar
. They are pointers to char; compare their values and those will differ by 1. You seem to have edited that out in your text.
您正在查看指针pointerToFirstChar和pointerToSecondChar的地址。它们是指向char的指针;比较它们的值,它们的值会相差1。你似乎已经在你的文章中编辑过了。
#3
0
you are printing address of pointer variable and not the address currently pointer is holding.
你打印的是指针变量的地址,而不是当前指针所在的地址。
for example:
例如:
&myCharString[0] = 0xFE20 &myCharString[1] = 0xFE21 &myCharString[2] = 0xFE23
char *pointerToFirstChar = &(myCharString[0]);
Address of pointerToFirstChar = 0xF8C2 and it holds the address &myCharString[0] = 0xFE20
pointerToFirstChar = 0xF8C2的地址,它保存地址&myCharString[0] = 0xFE20。
so you are printing, 0xF8C2 rather than printing 0xFE20
所以你要打印,0xF8C2而不是打印0xFE20。
Update your code as follows to get the correct result.
按照以下方法更新代码以获得正确的结果。
cout << "Value was " << pointerToFirstChar << ", address: " << (void *)&pointerToFirstChar[0] << endl; cout << "Value 2 was " << pointerToSecondChar << ", address:" << (void *)&pointerToSecondChar[0] << endl;
[0]<< < / span > < span lang = en - us style = ' font - size: 9.0 pt; font - family:宋体;[0]<< / span > < span lang = en - us style = ' font - size: 9.0 pt; font - family:宋体;
For more information please follow the below link print address of array of char
有关更多信息,请遵循以下链接打印地址数组的char。
#1
5
&pointerToFirstChar
and &pointerToSecondChar
, you're not taking the address of the elements of the char
array, but the address of the local variable pointerToFirstChar
and pointerToSecondChar
. Note they've been pointers themselves.
&pointerToFirstChar和&pointerToSecondChar,您没有获取char数组元素的地址,而是本地变量pointerToFirstChar和pointerToSecondChar的地址。注意,它们都是指针。
You might want:
你可能会想:
cout << "Value was " << pointerToFirstChar << ", address: " << static_cast<void*>(pointerToFirstChar) << endl;
cout << "Value 2 was " << pointerToSecondChar << ", address:" << static_cast<void*>(pointerToSecondChar) << endl;
Note you need to cast them to void*
to print out the address instead of the string.
注意,您需要将它们转换为void*以打印地址而不是字符串。
#2
0
You're looking at the addresses of the pointers pointerToFirstChar
and pointerToSecondChar
. They are pointers to char; compare their values and those will differ by 1. You seem to have edited that out in your text.
您正在查看指针pointerToFirstChar和pointerToSecondChar的地址。它们是指向char的指针;比较它们的值,它们的值会相差1。你似乎已经在你的文章中编辑过了。
#3
0
you are printing address of pointer variable and not the address currently pointer is holding.
你打印的是指针变量的地址,而不是当前指针所在的地址。
for example:
例如:
&myCharString[0] = 0xFE20 &myCharString[1] = 0xFE21 &myCharString[2] = 0xFE23
char *pointerToFirstChar = &(myCharString[0]);
Address of pointerToFirstChar = 0xF8C2 and it holds the address &myCharString[0] = 0xFE20
pointerToFirstChar = 0xF8C2的地址,它保存地址&myCharString[0] = 0xFE20。
so you are printing, 0xF8C2 rather than printing 0xFE20
所以你要打印,0xF8C2而不是打印0xFE20。
Update your code as follows to get the correct result.
按照以下方法更新代码以获得正确的结果。
cout << "Value was " << pointerToFirstChar << ", address: " << (void *)&pointerToFirstChar[0] << endl; cout << "Value 2 was " << pointerToSecondChar << ", address:" << (void *)&pointerToSecondChar[0] << endl;
[0]<< < / span > < span lang = en - us style = ' font - size: 9.0 pt; font - family:宋体;[0]<< / span > < span lang = en - us style = ' font - size: 9.0 pt; font - family:宋体;
For more information please follow the below link print address of array of char
有关更多信息,请遵循以下链接打印地址数组的char。