- 基本类型
- 数组
- 指针
- 函数原型
- 函数指针
除了不能定义函数原型的数组外,其他的都可以相互组合,千变万化,尤其是函数指针与数组的结合。
变量,char型
char a, size :1
数组,元素是3个char型变量
char b[3], size :3
指针,指向char型变量
char *c, size :4
数组,元素是3个指针,指针指向char型变量
char *d[3], size :12
指针,指向数组,数组包含3个char型变量元素
char (*e)[3], size :4
^注意这里e有了单独的括号,强调了他的类型是指针,可以与对照来看
函数原型,输入short型,返回char型
char f(short arg), size :cannot use sizeof
指针,指向函数,函数输入short型,返回char型
char (*g)(short arg), size :4
^类比e,有了单独的括号(*),强调了是指针,指向一个函数原型,可以叫它函数指针
数组,数组元素是3个指针,指向函数,函数输入short型,返回char型
char (*h[3])(short arg), size :12
^类比d,它是数组,变量名右边紧挨[3]
函数原型,输入short型,返回char*型
char *i(short arg), size :cannot use sizeof
函数原型,输入short型,返回char**型
char **j(short arg), size :cannot use sizeof
指针,指向函数,函数输入short型,返回char*型
char *(*k)(short arg), size :sizeof(k)
指针,指向数组,数组有3个元素,元素是指针,指针指向函数,函数输入short型,返回char型
har (*(*l)[3])(short arg), size :4
^分析最里面的括号,首先它是指针,其次右边紧挨[3],说明指向数组,最后又是一个(*),说明数组的元素是指针,而且是函数指针!
指针,指向函数,函数输入short型,返回char(*)[3]型
char (*(*m)(short arg))[3], size :4
^同样最里面是(*m),说明m是指针,右边紧挨括号参数,说明是函数指针,函数的返回类型比较奇怪是char(*)[3],见char (*e)[3]
数组,数组有3个元素,元素是函数指针的指针,函数指针指向函数,函数输入short型,返回char型
char (**n[3])(short arg), size :12
^可以 写成char (*(*n[3]))(short arg),所以从内向外解小括号,是数组,是指针,是函数指针,得到是函数指针的指针的数组!
此外还有与const的结合,使用typedef等,TBC
#include <iostream>
using namespace std;
int main()
{
char a;
cout << ">变量,char型" << endl;
cout << "char a, size :" << sizeof(a) <<endl<<endl;
char b[3];
cout << ">数组,元素是3个char型变量" << endl;
cout << "char b[3], size :" << sizeof(b) <<endl<<endl;
char *c;
cout << ">指针,指向char型变量" << endl;
cout << "char *c, size :" << sizeof(c) <<endl<<endl;
char *d[3];
cout << ">数组,元素是3个指针,指针指向char型变量" << endl;
cout << "char *d[3], size :" << sizeof(d) <<endl<<endl;
char (*e)[3];
cout << ">指针,指向数组,数组包含3个char型变量元素" << endl;
cout << "char (*e)[3], size :" << sizeof(e) <<endl<<endl;
char f(short arg);
cout << ">函数原型,输入short型,返回char型" << endl;
cout << "char f(short arg), size :" << "cannot use sizeof" <<endl<<endl;
char (*g)(short arg);
cout << ">指针,指向函数,函数输入short型,返回char型" << endl;
cout << "char (*g)(short arg), size :" << sizeof(g) <<endl<<endl;
char (*h[3])(short arg);
cout << ">数组,数组元素是3个指针,指向函数,函数输入short型,返回char型" << endl;
cout << "char (*h[3])(short arg), size :" << sizeof(h) <<endl<<endl;
char *i(short arg);
cout << ">函数原型,输入short型,返回char*型" << endl;
cout << "char *i(short arg), size :" << "cannot use sizeof" <<endl<<endl;
char **j(short arg);
cout << ">函数原型,输入short型,返回char**型" << endl;
cout << "char **j(short arg), size :" << "cannot use sizeof" <<endl<<endl;
char *(*k)(short arg);
cout << ">指针,指向函数,函数输入short型,返回char*型" << endl;
cout << "char *(*k)(short arg), size :" << "sizeof(k)" <<endl<<endl;
char (*(*l)[3])(short arg);
cout << ">指针,指向数组,数组有3个元素,元素是指针,指针指向函数,函数输入short型,返回char型" << endl;
cout << "har (*(*l)[3])(short arg), size :" <<sizeof(l) <<endl<<endl;
char (*(*m)(short arg))[3];
cout << ">指针,指向函数,函数输入short型,返回char(*)[3]型" << endl;
cout << " char (*(*m)(short arg))[3], size :" <<sizeof(m) <<endl<<endl;
char (**n[3])(short arg);
cout << ">数组,数组有3个元素,元素是函数指针的指针,函数指针指向函数,函数输入short型,返回char型" << endl;
cout << " char (**n[3])(short arg), size :" <<sizeof(n) <<endl<<endl;
return 0;
}
网友评论