typedef 的作用就是起别名
ex1. typedef int integer;
integer a=10;
ex.2 typedef int *PTR // 相当于 把int* 起别名为PTR 。
typedef 进阶
int (*ptr)[3] // 意思是 定义了一个指向有3整形元素的数组的指针。
// 相当于 int a[3]; int *ptr ; ptr = a;
ex,1
typedef int (*PTR_TO_ARRAY)[3]; 也就是说想要定义一个指向三个整形元素的数组的指针。
即PTR_TO_ARRAY就是 int ( * )【3】
再使用PTR_TO_ARRAY去定义一个指针就行了。(这个指针是指向整个数组的指针,所以要把&array赋过去,而不是直接array,因为array是数组的首地址,&array是整个数组的地址。
所以
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
typedef int(*PTR_TO_ARRAY)[3];
int main()
{
int array[3] = { 1,2,3 };
int i = 0;
PTR_TO_ARRAY ptr_to_array = &array;
for (i = 0; i < 3; i++)
{
printf("%d\n", (*ptr_to_array)[i]);
}
return 0;
system("pause");
}