指针数组
只看后面两字,就是一个数组,再看前面两字,可知道数组元素为指针类型(地址)。
例如:
这里[ ]的运算符比 * 高,所以先是a[5]确定为数组,在看int *,为指针类型的数组。
理解:开辟一个空间,空间内5个都为指针(地址),且该指针所指向的内容读取方式为int。
int* a[5];
例程:
#include <stdio.h>
int main()
{
/* Write C code in this online editor and run it. */
int a1 = 1;
int a2 = 2;
int* a[2] = {&a1,&a2}; //指针数组保存着两个地址
printf("*a[0] = %d , *a[1] = %d ",*a[0],*a[1]);
return 0;
}
输出为 *a[0] = 1 , *a[1] = 2
数组指针
就是一个指针,类型为数组,一个指向数组类型空间的指针。
例如:
这里( )的运算符比[ ]高,所以先是(*a)确定为指针,在看int [5],为数组类型的指针。
int (*a)[5];
例程:
#include <stdio.h>
int main()
{
/* Write C code in this online editor and run it. */
int arry[3][2] = {1,2,
3,4,
5,6}; //三行两列的数组
int(*a)[2] = arry; //指向一行数组的指针
printf("(*a)[0] = %d , (*a)[1] = %d \r\n",(*a)[0],(*a)[1]);
++a; //换行
printf("(*a)[0] = %d, (*a)[1] = %d",(*a)[0] ,(*a)[1]);
return 0;
}
输出为
(*a)[0] = 1 , (*a)[1] = 2
(*a)[0] = 3, (*a)[1] = 4