简体   繁体   English

2D数组指针-访问元素和地址

[英]2d array pointers - accessing elements and address

I am learning 2d array pointers and here is my code. 我正在学习2d数组指针,这是我的代码。 I donot know why this line: 我不知道为什么这行:

cout<<"Address of 1st part = "<<*ptr`  

is not showing an address while this line is showing me address: 此行显示我地址时未显示地址:

cout<<"Address of 1st part = "<<*(A)`  

These both lines means same can any one help me. 这两条线意味着任何人都可以帮助我。

#include <iostream>
using namespace std;


int main()
{
    int A[2][3]={{1,2,4},{5,8,3}};

    int *ptr;
    ptr=&A[0][0];

    cout<<"Address 1st part = "<<A<<endl;
    cout<<"Address 2nd part = "<<A+1<<endl;

    cout<<"Address 1st part = "<<ptr<<endl;
    cout<<"Address 2nd part = "<<ptr+1<<endl;

    cout<<"Address of 1st part = "<<*(A)<<endl;
    cout<<"Address of 1st part = "<<*ptr<<endl;

    cout<<"Address"<<*(A+1)+1<<endl;

    cout<<*(A+1)+2<<endl;

    return 0;
}

output 产量

Address 1st part = 0x7fffb6c5f660 
Address 2nd part = 0x7fffb6c5f66c 
Address 1st part = 0x7fffb6c5f660 
Address 2nd part = 0x7fffb6c5f664 
Address of 1st part = 0x7fffb6c5f660 
Address of 1st part = 1 
Address0x7fffb6c5f670 
0x7fffb6c5f674

Those two lines do not actually mean the same. 这两行实际上并不意味着相同。 A multi-dimensional array is not equivalent to a pointer to its primitive type. 多维数组不等同于指向其原始类型的指针。

A is of type int [2][3] , which is equivalent to int *[3] . A的类型为int [2][3] ,等效于int *[3] The type of *A is int[3] , not int . *A的类型是int[3] ,而不是int The step between successive pointed-to elements, sizeof *A , is equal to sizeof(int)*3 . 连续指向的元素之间的步长sizeof *A等于sizeof(int)*3

ptr is of type int * . ptr的类型为int * The type of *ptr is int . *ptr的类型为int The step here, sizeof *ptr , is equal to sizeof(int) . 此处的步骤sizeof *ptr等于sizeof(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM