C语言指针用法总结
1.指针的两种赋值方式
//第一种方式
int a = 1;
int *p;
p = &a;
printf("%p\n",p); //输出地址
printf("%d\n",*p); //输出所指地址内容
//第二种
printf("第二种\n");
int b = 1;
int *p2 = &b;
printf("%p\n",p2);
printf("%d\n",*p2);
2.结构体指针
//定义一个结构体
struct node{
float x,y;
char s[10];
}point;
定义结构体指针的两种方式:
//定义一个结构体
struct node{
float x,y;
char s[10];
}point,*p = &point; //*p为该结构体指针
//或者
struct node *p;
p = &point; //类似于1中的方式
3.结构体中".“和”->"的使用
对于普通结构体变量,两者均可。
对于结构体指针,用"->"访问,不能用"."访问
访问方式为:
struct node *p;
p = &point;
//p.y = 3.0; //报错
(*p).x = 2.0; //不会报错
p->y = 3.0;
printf("%f ,%f ",point.x,point.y);