数组地址,c++数组地址

用C++写程序程序都几年了,自己学得也算得心应手。不过最近在看别人写的代码时,觉得自己对C++的不是很深入的了解,很多东西都已经忘了。最拿C++数组地址来说。
#include
typedef struct { int a; int b; }TESTSTRUCT;
TESTSTRUCT g_TestStruct[5];
int main(int argc, char *argv[]) { printf("sizeof(TESTSTRUCT) = 0x%x\n", sizeof(TESTSTRUCT)); printf("[g_TestStruct] 's adddress 0x%x\n", g_TestStruct); printf("[g_TestStruct + 5] address 0x%x\n", g_TestStruct + 5); printf("[(void *)g_TestStruct + 5] address 0x%x\n", (void *)g_TestStruct + 5); return 0; }
上面一段简单的程序c++数组地址的计算方法。数组地址直接加数字[g_TestStruct + 5],一眼看上去好像是g_TestStruct的虚拟地址再加上5的偏移。不过这是错的,C++中是这样处理的,g_TestStruct的虚拟地址加5 *sizeof(TESTSTRUCT)。
运行的结果也这样的
imagec++数组地址数组地址
Tags:  数组名地址 数组的地址 二维数组地址 取数组地址 数组地址

延伸阅读

最新评论

发表评论