数组元素值,元素地址和指针增量
Array element values, element addresses, and pointer increment
这是一个非常基本的概念。但是,我普遍感到困惑。帮我。
案例1:
对于以下代码:
int oneDArray[] = {1,2,3};
cout<<&oneDArray<<endl;
cout<<oneDArray<<endl;
cout<<&oneDArray+1<<endl;
cout<<oneDArray+1<<endl;
输出是:
0x28fef4
0x28fef4
0x28ff00
0x28fef8
为什么增量值有差异?
案例2:
int arr[2][3] = {{1,2,3}, {4,5,6}};
cout<<&arr<<endl;
cout<<arr<<endl;
cout<<*arr<<endl;
cout<<&arr+1<<endl;
cout<<arr+1<<endl;
cout<<*arr+1<<endl;
输出是:
0x28fee8
0x28fee8
0x28fee8
0x28ff00
0x28fef4
0x28feec
为什么ARR&amp;的输出相同*arr?(内部工作原理)为什么增量值有差异?
用于单维数组:
//starting at memory address: 0x28FEF4
int My_Array[3] = {1,2,3};
//memory dumping, each 'int' element takes 4 bytes,
//high bytes are at higher addresses
0x28FEF4 01 00 00 00
0x28FEF8 02 00 00 00
0x28FEFC 03 00 00 00
这是从C语言到英语的翻译:
My_Array = (address) of-the (first-byte) of (My_Array)
My_Array[0] = (value) of-the (element-at-index-0)
My_Array[k] = (value) of-the (element-at-index-k)
&My_Array = (address) of-the (first-byte) of (My_Array)
&My_Array[0] = (address) of-the (first-byte) of (element-at-index-0)
&My_Array[k] = (address) of-the (first-byte) of (element-at-index-k)
*My_Array = (value) pointed-by (My_Array)
*My_Array[0] = (value) pointed-by (My_Array[0])
*My_Array[k] = (value) pointed-by (My_Array[k])
The values of 'My_Array' and '&My_Array' are always identical for array type
but there's a difference when using them with mathematical operators.
My_Array is a pointer to 'int' type, while &My_Array is a pointer
to 'int[3]' type. When incremented, the values added in are sizeof(int)
and sizeof(int[3]) respectively.
The last two notations: *My_Array[0] and *My_Array[k]
are valid in mathematical sense but not valid in C language when using
with single-dimension array because C langugae doesn't allow using
'int' value as pointer.
说明您的情况1的输出结果:
(&My_Array) is identical to (My_Array) =
0x28FEF4
(My_Array) is identical to (&My_Array) =
0x28FEF4
(&My_Array+1) = 0x28FEF4 + sizeof(int[3]) = 0x28FEF4 + 12 =
0x28FF00
(My_Array+1) = 0x28FEF4 + sizeof(int) = 0x28FEF4 + 4 =
0x28FEF8
将上面的笔记应用于您的第二种情况,以及这些注意事项:
(1) Value of element in single dimension array is the base type. Eg:
int My_Array[3]; //base type is 'int'
(2) Value of element in multiple dimension array is either pointer
or base type. Eg:
int My_Array[2][3][4]; //base type is 'int'
My_Array is a pointer to 'int' type
My_Array[i] is a pointer to 'int' type
My_Array[i][j] is a pointer to 'int' type
My_Array[i][j][k] is an 'int' value
&My_Array is a pointer to 'int[2][3][4]'
&My_Array[i] is a pointer to 'int[3][4]'
&My_Array[i][j] is a pointer to 'int[4]'
&My_Array[i][j][k] is a pointer to 'int'
(3) Values of 'My_Array' and '&My_Array' are always equal,
however, when dealing with operators, they work differently.
Eg.
int My_Array[2][3][4]; //base type is 'int'
'&My_Array' here is a pointer to 'int[2][3][4]', but
'My_Array' is a pointer to base type 'int'.
(4) A pointer is always incremented by the size of the type
that it points to. 'int' has size of 4 bytes.
Eg.
int My_Array[2]; //type size = (2)*4
int My_Array[2][3]; //type size = (2*3)*4
int My_Array[2][3][4]; //type size = (2*3*4)*4
int My_Array[2][3][4][5]; //type size = (2*3*4*5)*4
相关文章:
- CUDA:统一内存和指针地址的更改
- 是否可以仅通过将分配的指针地址存储在C++中来分析内存?
- 向指针地址添加 20 个字节偏移量
- 如何理解智能指针的底层指针地址?
- 初学者问题:C++指针/地址 - 和变量之后不是以前?
- 返回数组中值的指针地址
- C++向指针地址添加 4 个字节
- 指针地址的内存偶尔更改了函数前和后返回
- 如何根据指针地址获取指针内容
- C++ 将指针地址传递给函数
- 直接从指针/地址访问数组的元素
- 如何在没有作弊引擎的情况下从其他应用程序获取指针地址?
- 如何保存指针地址,以便另一个指针可以继续处理它
- 间接寻址运算符如何返回带有运算符重载的指针地址
- 使用已删除的指针地址.据我所知,
- 如何保护类外用户可以修改的指针地址?C++
- 多个调用构造函数将更改C 中的成员指针地址
- 类对象指针的向量:如何在指针地址获得价值?C 和SDL_RECT
- Visual Studio C 2K15-在指针地址上获取错误
- 指针地址和参考混乱