【待完善】數組和指針的區別 指針和引用的區別

(a) 一個整型數(An integer)

(b) 一個指向整型數的指針(A pointer to an integer)

(c) 一個指向指針的的指針,它指向的指針是指向一個整型數(A pointer to a pointer to an integer)

(d) 一個有10個整型數的數組(An array of 10 integers)

(e) 一個有10個指針的數組,該指針是指向一個整型數的(An array of 10 pointers to integers)

(f) 一個指向有10個整型數數組的指針(A pointer to an array of 10 integers)

(g) 一個指向函數的指針,該函數有一個整型參數並返回一個整型數(A pointer to a function that takes an integer as an argument and returns an integer)

(h) 一個有10個指針的數組,該指針指向一個函數,該函數有一個整型參數並返回一個整型數( An array of ten pointers to functions that take an integer argument and return an integer )

答案:

(a) int a; // An integer

(b) int *a; // A pointer to an integer

(c) int **a; // A pointer to a pointer to an integer

(d) int a[10]; // An array of 10 integers

(e) int *a[10]; // An array of 10 pointers to integers

(f) int (*a)[10]; // A pointer to an array of 10 integers

(g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer

(h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

 

(1)指針數組:它實際上是一個數組,數組的每個元素存放的是一個指針類型的元素。

int* arr[8];
//優先級問題:[]的優先級比*高
//說明arr是一個數組,而int*是數組裏面的內容
//這句話的意思就是:arr是一個含有8和int*的數組

(2)數組指針:它實際上是一個指針,該指針指向一個數組。

int (*arr)[8];
//由於[]的優先級比*高,因此在寫數組指針的時候必須將*arr用括號括起來
//arr先和*結合,說明p是一個指針變量
//這句話的意思就是:指針arr指向一個大小爲8個整型的數組。

 

四、函數指針、函數指針數組、函數指針數組的指針

1.函數指針

void test()
{
    printf("hehe\n");
}
//pfun能存放test函數的地址
void (*pfun)();


函數指針的形式:類型(*)( ),例如:int (*p)( ).它可以存放函數的地址,在平時的應用中也很常見。

2.函數指針數組

形式:例如int (*p[10])( ); 
因爲p先和[ ]結合,說明p是數組,數組的內容是一個int (*)( )類型的指針 
函數指針數組在轉換表中應用廣泛

3.函數指針數組的指針

指向函數指針數組的一個指針,也就是說,指針指向一個數組,數組的元素都是函數指針

void test(const char* str)
{
    printf("%s\n", str);
}
int main()
{
    //函數指針pfun
    void (*pfun)(const char*) = test;
    //函數指針的數組pfunArr
    void (*pfunArr[5])(const char* str);
    pfunArr[0] = test;
    //指向函數指針數組pfunArr的指針ppfunArr
    void (*(*ppfunArr)[10])(const char*) = &pfunArr;
    return 0;
}


 

 

https://blog.csdn.net/cherrydreamsover/article/details/81741459 數組和指針的區別與聯繫(詳細)

https://blog.csdn.net/qq_27678917/article/details/70224813  C++中指針和引用的區別

https://blog.csdn.net/qq_39539470/article/details/81273179 C++中指針和引用的區別詳解版

https://www.cnblogs.com/yinzm/p/6510938.html 指針和引用的區別

https://www.cnblogs.com/gxcdream/p/4805612.html 淺談指針和引用的區別

https://www.cnblogs.com/x_wukong/p/5712345.html  傳指針和傳指針引用的區別/指針和引用的區別(本質)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章