|
rules:
1. 指针是变量,该变量有类型 example: int * p;
2. 地址是一个值,该值是指针变量的值,该值有类型, example: (void*)0
3. 函数名是一个字符地址常量, 默认的类型为其对应的函数类型。 int a(int), int (*b)(int) b(指针)= a(与b同类型的函数的首地址)
4. 数组名是一个数组类型的字符地址常量,默认的类型为数组中下一维元素的类型。example:
int a[10], a 字符地址常量,默认为int型指针值
int a[10][10], a 字符地址常量,默认类型与a[0]相同 int (*b)[10]这中类型
5. 数组名取地址运算对应的地址的类型为数组型:
int a [10], int *b; int (*c)[10]; b=a yes; c=a no; c=&a yes;
6. 函数形参中声明的数组等于一个数组下一维元素类型的指针,如int fun(int a[])与int fun(int *a)一样;其中的a就是一个指针,不在理解为一个函数名,这是函数传递数组中的一种强制方法,编译器就是这样处理的,没为什么。函数中的a++运算有意思,因为a是一个变量,int a[10] a++运算没有意义,因为a是一个字符地址常量
example:
#include
int fun(int a[])
{
//infunction a = int *a;
printf("data team
name:\n");
printf("0: %d\n", *(a++));
printf("1: %d\n", *(a++));
printf("2: %d\n", *(a++));
}
int fun1(int *a)
{
//infunction a = int *a;
printf("data team element pointer
:\n");
printf("0: %d\n", *(a++));
printf("1: %d\n", *(a++));
printf("2: %d\n", *(a++));
}
int fun2(int a[])
{
//infunction a = int *a;
printf("data team
name:\n");
printf("0: %d\n", *(a++));
printf("1: %d\n", *(a++));
printf("2: %d\n", *(a++));
}
int fun3(int a[][3])
{
//infunction a = int (*a)[3];
printf("data team[3][3]
name:\n");
printf("0: %d\n", *(*(a)+0));
printf("0: %d\n", *(*(a)+1));
printf("0: %d\n", *(*(a)+2));
printf("1: %d\n", *(*(++a)+0));
printf("1: %d\n", *(*(a)+1));
printf("1: %d\n", *(*(a)+2));
printf("2: %d\n", *(*(++a)+0));
printf("3: %d\n", *(*(a)+1));
printf("3: %d\n", *(*(a)+2));
}
int fun4(int (*a)[3])
{
//infunction a = int (*a)[3];
printf("2data team[3][3]
name:\n");
printf("0: %d\n", *(*(a)+0));
printf("0: %d\n", *(*(a)+1));
printf("0: %d\n", *(*(a)+2));
printf("1: %d\n", *(*(++a)+0));
printf("1: %d\n", *(*(a)+1));
printf("1: %d\n", *(*(a)+2));
printf("2: %d\n", *(*(++a)+0));
printf("3: %d\n", *(*(a)+1));
printf("3: %d\n", *(*(a)+2));
}
int main()
{
int a[]={1,2,3};
int b[3][3]={{1,2,3},{4,5,6},{7,8,9}};
double c;
int* p2;
char *p;
int (*p1)[];//team type ponter
p =(char*)a;//type problem
p= (char*)&c;//type problem
p1 = &a;// team tyep to team type
pointer
p2 = a;// a defualt type is element type
//p2 = &a; cause type error, team
pointer assign element type
fun(a);
fun1(a);
//fun2(b);//b is int (*p)[3]type , a is
int*p
fun3(b);
fun4(b);
return(0);
}
result:
[zhliu@sdcsn05 ~]$ gcc -o test41 test41.c
[zhliu@sdcsn05 ~]$ test41
data team name:
0: 1
1: 2
2: 3
data team element pointer :
0: 1
1: 2
2: 3
data team[3][3] name:
0: 1
0: 2
0: 3
1: 4
1: 5
1: 6
2: 7
3: 8
3: 9
2data team[3][3] name:
0: 1
0: 2
0: 3
1: 4
1: 5
1: 6
2: 7
3: 8
3: 9