据说看懂这道题,就学会指针了......分享给大家看看

2020-02-03 10:12发布

指针的使用一直都是初学者的第一道坎儿,绕来绕去的。。。以下这道题,据说琢磨透了,就基本不怕指针了..~~

用变量a给出下面的定义
a) 一个整型数
b)一个指向整型数的指针( A pointer to an integer)
c)一个指向指针的的指针,它指向的指针是指向一个整型数( A pointer to a pointer to an intege)r
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]; // 一个有10个整型数的数组 An array of 10 integers
e) int *a[10]; // 一个有10个指针的数组 An array of 10 pointers to integers
f) int (*a)[10]; // 一个指向有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); // 一个有10个指针的数组,指向一个整形函数并有一个整形参数 An array of 10 pointers to functions that take an integer argument and return an integer
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
99条回答
914775866
2020-02-14 08:50
相信很多人对(int *a[10])( int ) 中 a 指针的定义感到困惑,我初看此题也想了很久,其实该指针的定义跟 int *a[10]本质上是相同的,我认为编译器在语法处理上是一致的,只是前者数组指针存储的内容为函数入口地址,后者数组指针存储的内容为整型数据。对前者举例如下:

int myfun( int me )
{
   return me;
}

//函数指针数组类型定义
typedef  (int *fun[10])(int);

定义 函数指针数组变量:  fun   ptrFun;
对指针数组变量赋值:   ptrFun[0] = myfun;
通过变量间接调用函数:  (*ptrFun[0])( 168 );

一周热门 更多>