• int*p[ ]、int(*p)[ ]、int(**p)[]的不同


    指针数组的概念:

    一个数组,若其元素均为指针类型数据,称为指针数组,也就是说指针数组中的每一个元素都相当于一个指针变量。定义如下:

    类型名 * 数组名[ 数组长度 ]

    由于[ ]比*优先级高,因此上述定义等价于( 类型名 * ) 数组名[ 数组长度 ].

    数组的指针的概念:

    类型名 (* 数组名) [ 数组长度 ]

    举例说明:

    1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型。

    它是 一个 行数确定、列数不确定,即为2*n型的。

    可以这样来用:

    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        int* p[2];
        int a[3] = {1, 2, 3};
        int b[4] = {4, 5, 6, 7};
    
        p[0] = a;
        p[1] = b;
    
        for(int i = 0; i < 3; i++)
            cout << *( p[0] + i );
         // cout << *( *p + i );
    cout << endl;
    for(int i = 0; i < 4; i++) cout << *( p[1] + i ); // cout << *(*( p + 1 ) + i ); return 0; }

    2)对于 int (*p)[2], 它相当于一个二维数组的用法,只是它是一个n行2列的数组,

    它是n*2型的数组的指针用法,即行数不确定、列数确定。可以这样来用:

    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    
        int (*p)[2];
        int b[3][2] = {{1, 2}, {3, 4}, {5, 6}};
        p = b;
        for(int i = 0; i < 3; i++) 
        {
            for(int j = 0; j < 2; j++) 
                cout << p[i][j]; 
                //cout << *(*(p+i)+j);
            cout << endl;
        }
    
        return 0;
    }

    对与1)其等价形式:

    #include <iostream>
    using namespace std;
    
    int main(void)
    {
        int** array;
        array = new int* [2];
    
        int a[3] = {1, 2, 3};
        int b[4] = {4, 5, 6, 7};
    
        array[0] = a; // *array = a;
        array[1] = b; // *(array+1) = b;
    
        for(int i = 0; i < 3; i++) 
            cout << array[0][i];
         // cout << *array[0] + i;
    
        cout << endl;
    
        for(int j = 0; j < 4; j++) 
            cout << array[1][j];
        // cout << *array[1] + j;
        return 0;
    }                

    3)int(**p)[]

    这个的意义:q是一个指针,指向的元素就是2)中的p.

    #include<iostream>
    #include<stdio.h>
    using namespace std;
    
    int main(void)
    {
        int a[2][2]={1,2,3,4};
        int (*p)[2] = a;//p point to the row of array a 
        for(int i = 0;i<2;i++)//output matrix using p 
        {
                for(int j = 0;j<2;j++)
                {
                        cout<<*(*(p+i)+j)<<" "; 
                }
                cout<<endl;
        }
        int (**q)[2] = &p;//q point to p
        for(int i = 0;i<2;i++)//output matrix using q 
        {
                for(int j = 0;j<2;j++)
                {
                        cout<<*(*(*q+i)+j)<<" "; 
                }
                cout<<endl;
        }
        
        getchar();
    
        return 0;
    }
  • 相关阅读:
    浏览器兼容
    jquery 判断网络资源,网络文件是否存在
    [Maven] 变态问题收集
    maven project中,在main方法上右键Run as Java Application时,提示错误:找不到或无法加载主类XXX.XXXX.XXX
    maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project web_nanchang
    java 获取文件列表,并按照文件名称排序
    maven install时报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
    tar 命令基本使用(加密)
    八卦某 G 的前端开发方式及流程
    use mkisofs 重新打包beini,tinycore linux
  • 原文地址:https://www.cnblogs.com/theCambrian/p/3650168.html
Copyright © 2020-2023  润新知