指標與數組——二級指標 數組初始化 數組列印 動態二維數組

來源:互聯網
上載者:User

標籤:刪除   sizeof   bool   tar   儲存   row   int   delete   相同   

        int a = 10;    int *p = &a;    int **pp = &p;    printf("%d\t%d\n", *p, **pp);  // 10   10    
//數組初始化:    int a[4];//裡面為未知值    int a[4] = { 0 }; //初始化為0
//二維數組指標    int arr1[3][4] = { 0 };    int(*pArr)[4] = NULL;    pArr = arr1;    for (int i = 0; i < 3; i++)    {        for (int j = 0; i < 4;j++)        {            //下面三中表示模式完全相同            *(*(pArr + i) + j) = i * 4 + j;            pArr[i][j] = i * 4 + j;            arr1[i][j] = i * 4 + j;        }            }
//一級指標    int *p = (int *)malloc(12 * sizeof(int));    int index = 0;    for (int i = 0; i < 3; i++)    {        for (int j = 0; j < 4; j++)        {            *(p + index++) = i * 4 + j;        }    }    //對應的輸出為    printf("%d\t", *(p + i * 4 + j));
    //二級指標    int **pArr4 = NULL;    *pArr4 = (int *)malloc(4 * sizeof(int *));    for (int i = 0; i < 4; i++)    {        *(pArr4 + i) = (int *)malloc(4 * sizeof(int));    }
//&三種用法    //1、取地址運算子,需要一個運算元    //2、位與運算子,需要兩個運算元    //3、引用,左邊需要類型    int *pArr1[4];//一個數組,裡面存放全是int *指標    for (int i = 0; i < 4; ++i)    {        pArr1[i] = (int *)malloc(4 * sizeof(int));    }
    //一維數組的列印    int* GetArr(int arr[])    {        return arr;    }    int arr1[4] = { 1, 2, 3, 4 };    int *p = GetArr(arr1);    for (int i = 0; i < 4;++i)    {        printf("%d\n", *(p + i));    }
//二維數組在記憶體中的儲存也是按照一維數組的方式儲存的    //列印二維數組    int * GetArr_double(int arr[][4])    {        return &arr[0][0];    }    int arr[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };    int *p = GetArr_double(arr);    for (int i = 0; i < 12;++i)    {        printf("%d\n", *(p + i));    }
//typedef類型重定義    //需要返回二維數組,但是不降維    typedef int(*pArr)[4]; //為一個二維數組指標,可以指向所有列為四的資料    pArr GetArr_d(int arr[][4])   //二維數組不進行降維處理,直接進行重定義    {        return arr;    }    int arr[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };    pArr pa = GetArr_d(arr);    for (int i = 0; i < 3;i++)    {        for (int j = 0; j < 4; ++j)        {            printf("%d\n", pa[i][j]);        }    }
//typedef類型重定義   重定義的是類型 數組不是類型    typedef char NAME[32];    typedef int(*pArr)[4]; //二維數組  pArr是類型    typedef bool(*pFun)(int, int);    //函數指標 pFun是類型    bool isMax(int a, int b)    {        return a > b;    }    void main()    {        NAME n1 = "hello"; //這樣定義出來的 n1就是一個數組        pArr pa = NULL;        pFun pf = NULL;        pFun pf1 = isMax;    //pf1是一個函數指標,可以指向一個函數,函數名即為首地址                    //注意  該函數沒有括弧!!!    }
    int row, col;    scanf("%d%d", &row, &col);    //動態二維數組    int **p = new int *[row];    for (int i = 0; i < row; i++)        *(p + i) = new int[col];    for (int i = 0; i < row; i++)    {        for (int j = 0; j < col; j++)        {            *(*(p + i) + j) = i*col + j + 1; //隨便賦的值        }    }    for (int i = 0; i < row; ++i)    {        delete[] * (p + i);//刪除底層指標    }    delete[] p; //刪除上層指標

 

指標與數組——二級指標 數組初始化 數組列印 動態二維數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.