指针数组与数组指针

来源:互联网 发布:哪个软件看泰剧最全 编辑:程序博客网 时间:2024/06/08 06:15

c语言中的数组有自己特 定的类型

数组的类型由元素类型和数组大小共同决定   如:int array[5]的类型为int[5]


定义数组类型:

    通过typedet type(name)[size];为数组类型重命名

数组类型:typedef int(AINT5) 【5】;

则数组定义为:AINT5 array;


数组指针用于指向一个数组

#include<stdio.h>typedef int(AINT5) [5];typedef float(AFLOAT10) [10];typqdef  char(ACHAR9) [9]; int main(){AINT5 a1;float fArray[10];AFLOAT10* pf=&fArray;ACHAR cArray;char(*pc)[9]=&cArray;char(*pw)[4]=cArray;  //error:这用用一个指向数组的指针去指向一个char类型的地址,这是不合法的!!!
int i=0;printg("%d , %d\n",sizeof(AINT5),sizeof(a1));//这里算的是算出类型所占用的内存空间的大小,都是20 for(i=0;i<10;i++){(*pf)[i]=i;//等价于fArray[i]=i;}printf("%p,%p,%p",&cArray,pc+1,pw+1);//  pc+1===>(unsigned int)pc+sizeof(*pc)==>(unsigned int)pc+9}

数组名是数组首元素的起始地址,但并不是数组的起始地址

通过将取地址符&作用于数组名可以得到数组的起始地址

可通过数组类型定义数组指针:ArrayType* pointer;

也可直接定义:type(*pointer)[n];

      pointer为数组指针变量名

     type为指向的数组的类型

    n为指向的数组的大小