一、初始指针
int *p;
p=&a;
*p是值,p是地址
双重指针 **pp;
pp=&p;
二、指向数组的指针
int a[5],int *p=a;
p、a、&a[0]指向a的首地址a[0]
p是变量,而a,&a[i]是常量;
指针变量访问a数组元素 p[i],*(p+i)
指针可以看出数组名
int *a;
a=new int[n-1];
三、指针与字符串(指针存储字符串)
字符指针变量可以用字符串常量初始化
char *str="I love China!";
输出要用:printf("%s",str);
字符串指针作为函数参数,可以用来函数传递另一个函数。
void test(char* str){}
int main(){
char s[100];
char *str=s;
test(str); //函数调用
}
四、指针作为函数参数
void test(int *x){//指针做参数
int t = *x;
cout<<t;
}
int main(){
int a=5;
test(&a); //调用函数
}
五、函数返回指针(函数可以返回数字,指针同样也可以)
函数定义:int *test(int a){}
函数调用:int *p=test(5);
printf("%d%d",p,*p) //第一个地址、本身
六、函数指针
函数定义:int test(int a);
函数声明:int (*p)(int a);
获取函数地址:p=test;
函数调用:p(5); || (*p)(5);
typedef int (*LP)(int)
int main(){
LP p=test;
p(5); //调用函数
}
七、结构体指针
struct student{
char name[20];
char sex;
float score;
}; student *p;
指针名->成员名 p->sex
(*指针名).成员名 (*p).sex
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_44775255/article/details/124049909
内容来源于网络,如有侵权,请联系作者删除!