1、指针数组
指针数组的定义:type* pArray[n];
例如, int* pa[3]
- 指针数组中每个元素为一个指针
type*
为数组中每个元素的类型pArray
为数组名n
为数组大小
例如,
#include#include int lookup_keyword(const char* key, const char* table[], const int size){ int ret = -1; int i = 0; for(i=0; i
2、数组指针
数组指针用于指向一个数组。
- 数组名是数组首元素的起始地址,但并不是数组的起始地址
- 通过将取地址符&作用于数组名可以得到数组的起始地址,例如,
&array
- 可通过数组类型定义数组指针:
ArrayType* pointer;
例如,array int* pc
- 也可以直接定义:
type (*pointer)[n];
例如,int(*pc)[5]
pointer
为数组指针变量名type
为指向的数组的类型n
为指向的数组的大小
#includetypedef int(AINT5)[5];typedef float(AFLOAT10)[10];typedef char(ACHAR9)[9];int main(){ AINT5 a1; float fArray[10]; AFLOAT10* pf = &fArray; ACHAR9 cArray; char(*pc)[9] = &cArray; char(*pcw)[4] = cArray; int i = 0; printf("%d, %d\n", sizeof(AINT5), sizeof(a1)); for(i=0; i<10; i++) { (*pf)[i] = i; } for(i=0; i<10; i++) { printf("%f\n", fArray[i]); } printf("%0X, %0X, %0X\n", &cArray, pc+1, pcw+1);}
3、指针数组和数组指针的区别
1)指针数组
指针数组:指针数组可以说成是 ”指针的数组”,首先这个变量是一个数组。
其次,”指针”修饰这个数组,也就是说这个数组的所有元素都是指针类型。
在 32 位系统中,指针占四个字节。
2)数组指针
数组指针:数组指针可以说成是”数组的指针”,首先这个变量是一个指针。
其次,”数组”修饰这个指针,也就是说这个指针存放着一个数组的首地址,或者说这个指针指向一个数组的首地址。
所以指针数组和数组指针的区别,因为二者根本就是两种类型的变量。