1.数组类型
C语言中的数组有自己特定的类型,由元素类型和数组大小共同决定。
如:int array[5]的类型为int[5]
这个类型说明array数组有五个元素,每个元素都是int类型。
想想下面的对话对吗?
A∶这里定义的数组什么类型的?
B : int 型。
2.定义数组类型
C语言中,通过typedef为数组类型重命名。
格式为:typedef type(name)[size];
如
数组类型:
typedef int(AINT5)[5];
typedef float(AFLOAT10)[10];
数组定义:
AINT5 iarray;
AFLOAT10 farray;
有了数组类型之后我们就能够定义数组指针,就像我们有了int类型,可以定义int类型的指针,有float类型,可以定义float类型的指针,有结构体类型可以定义结构体类型的指针,现在我们有了数组类型,那么可以定义数组类型的指针,简称数组指针。
即:数组指针就是一个指针
3.数组指针
数组指针用于指向一个数组,数组名是数组首元素的起始地址,但并不是数组的起始地址,通过将取地址符&作用于数组名可以得到数组的起始地址,可通过数组类型定义数组指针: ArrayType* pointer;
也可以直接定义:type(*pointer_name)[n];
其中,pointer_name为数组指针变量名,type为指向的数组的类型,n为指向的数组的大小。
小tips:指针运算 p + n ;等价于(unsigned int )p + n*sizeof(*p)
示例代码
#include <stdio.h>
//重命名数组类型
typedef int(AINT5)[5];
typedef float(AFLOAT10)[10];
typedef char(ACHAR9)[9];
int main()
{
AINT5 a1;
float fArray[10];//定义一个数组
AFLOAT10* pf =