c++的數組指針、指針數組、引用數組、數組引用、


做一個簡單的小總結

注意最后面的詞語,前面的是修飾

數組指針

本質是一個數組
直接看代碼

#include <iostream>
using namespace std;

#define size 3
typedef int(*test_ptr)[size]; //test_ptr類型 int(*)[3] 代表一個數組大小為3 類型為int的數組指針

int main()
{
    int test_array[size]{1,2,3};
    test_ptr array_pt = &test_array; //把test_array指針傳給array_pt,現在array_pt是一個數組指針

    for(int i = 0; i < size; i++) {
        cout << (*array_pt)[i] << endl;  //首先相當於array_pt的地址取值變為test_array,然后就和正常的數組一樣使用
    }
    /*  1
        2
        3
    */
    //那么和指針指向一個數組的區別在哪
    int* low_array = test_array; //low_array的類型是int*,把test_array這個數組首元素賦值給low_array
    for(int i = 0; i < size; i++) {
        cout << low_array[i] << endl;  //low_array直接代表數組首元素所以可以直接遍歷
    }
    /*  1
        2
        3
    */
    //我們再來看一下指針加一的區別
    cout << "test_array  " << array_pt << endl;   //數組名代表數組首元素的地址
    cout << "array_pt  " << array_pt << endl;     //&test_array代表這個數組的存放地址是和數組首元素地址相同
    cout << "low_array  " << low_array << endl;   //數組首元素地址
    cout << "array_pt + 1  " << array_pt + 1 << endl; //類型為(int*)[3],array_pt + 1 = 0x61fe74 + 3 * sizeof(int) * 1 = 0x61fe80 
    cout << "low_array + 1  " << low_array + 1 << endl; //類型為 int* , low_array + 1 = 0x61fe74 + sizeof(int) * 1 = 0x61fe78
    /*
    test_array     0x61fe74
    array_pt       0x61fe74
    low_array      0x61fe74
    array_pt + 1   0x61fe80
    low_array + 1  0x61fe78
    */
    //可以看到初始地址都是相同的
    return 0;
}

圖解指針數組

和普通指針比較

數組指針和二維數組

其實本質來說和上面是一樣的,這里還是說一下

#include <iostream>
using namespace std;

#define size 3
typedef int(*test_ptr)[size]; //test_ptr類型 int(*)[3] 代表一個數組大小為3 類型為int的數組指針

int main()
{
    int test_array[2][size] = {
    {1,2,3},
    {2,3,4}
    };
    test_ptr array_pt = test_array; //test_array是二維數組的名字,等於第一個一維數組的地址
    //所以這里array_pt指向了第一個一維數組
    std::cout << (*array_pt)[0] << std::endl; //1
    std::cout << (*array_pt)[1] << std::endl; //2
    std::cout << (*array_pt)[2] << std::endl;//3
    std::cout << array_pt<< std::endl; //0x61fe74

    std::cout << sizeof(*array_pt)<< std::endl; //12


    array_pt++; //array_pt指針類型int(*)[3], 0x61fe74 + 1*12 = 0x61fe80

    std::cout << array_pt<< std::endl; //0x61fe80

    std::cout << (*array_pt)[0] << std::endl; //2
    std::cout << (*array_pt)[1] << std::endl; //3
    std::cout << (*array_pt)[2] << std::endl; //4
    return 0;
}

圖解

指針數組

根據開始的總結,指針數組是一個數組,存儲的都是指針

#include <iostream>
using namespace std;
int main()
{
    const char* a[3]{"12", "23", "45"};
    //a這個char類型的數組存儲了三個指向字符串的指針
    std::cout << a[0] << std::endl;
    //12
    std::cout << a[1] << std::endl;
    //23
    std::cout << a[2] << std::endl;
    //45
    return 0;
}

這個比較好理解,就是存儲的是指針

數組引用

根據第一個總結,這是一個引用,引用的是一個數組

#include <iostream>
using namespace std;
typedef int(&array_define)[3];
int main()
{
    int array[3]{1,2,3};
    array_define quote = array;
    //既然普通引用是變量直接賦值,那么數組引用也就是數組直接賦值
    //引用之后,對數組引用的操作和對數組的操作相同
    std::cout << quote[0] << std::endl;
    //1
    std::cout << quote[1] << std::endl;
    //2
    std::cout << quote[2] << std::endl;
    //3
    std::cout << quote + 1 << std::endl;
    //打印和array + 1相同的地址,數組首元素后移動4個字節

    //普通變量引用
    int a = 1;
    int quote_a = a;
    std::cout << quote_a << std::endl;
    //1

    return 0;
}

插一句題外話,關於指針地址相加,你可以想象變量、都是地址連續的排列的,那么指針向后移動,其實就是指針的地址向前走

引用數組

根據第一句總結,是一個數組,里面是引用
這里你是不是覺得和引用指針有點像
但是!!!!
c++不支持引用數組
原因是對於數組而言,數組存儲的變量地址都是連續的,但是引用是引用其他變量的,其他變量可能來自不同的地方,有不同的地址值
對引用取地址就是對變量取地址,那么這就和數組的根本定義矛盾了!!!

#include <iostream>
using namespace std;
int main()
{
    string& a[3] = {"2", "3", "4"};
    return 0;
}
//報錯
D:\Qt_test\untitled1\main.cpp:6: error: declaration of 'a' as array of references
     string& a[3] = {"2", "3", "4"};
                ^
錯誤:將a聲明引用數組

以上就是四種容易混淆的稱呼解釋

最后例子總結一下
int a = 1;//普通 int
char* b = "1";//普通char類型指針
int a[3]{1,2,3}; //類型為int[3]的數組
int(* aa) [3] = &a; //類型為int( * )[3]的指針,數組指針
char* aa[3]={"1","2","3"};//類型為int*[3]的數組,指針數組
int(&bb)[3] = a;//類型為int(&)[3]的引用,引用數組
//不存在的int& bb[3];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM