const int *p = &a;//定义常量指针,值不可改
int * const p = &a;//定义指针常量,指向的值可改,指向不可改
const为静态常量的意思,不可修改。从左到右,常量const先出现,指针符号*后出现,则称为常量指针,反之则称为指针常量。
如果是常量const先出现,则值不可改
如果是指针符号*先出现,则指向不可改
#include <iostream>
using namespace std;int main()
{int a=100; int b=10; const int *p = &a;//定义常量指针,值不可改 int * const p = &a;//定义指针常量,指向的值可改,指向不可改 cout<< &a <<endl;cout<< p <<endl;cout<< *q <<endl;//引用,拿内容 system("pause"); return 0;}
函数的形参void swap(int a,int b)和函数的形参是指针void(int *a,int *b)的区别。
void swap(int *a,int *b)在调用的时候,直接传入实参即可发送交换,打印出来。函数的形参void swap(int a,int b)需要在函数内部才可打印出来,在调用后打印无变化。
即写成void swap(int *a,int *b)可以方便实参的传递。
#include <iostream>
using namespace std;void swap(int *a,int *b) { int temp = *a;*a=*b;*b=temp;
}int main()
{int a =10;int b =60;//地址传递 swap(a,b);cout<<a<<"--"<<b<<endl;system("pause"); return 0;
}
结构体是我们自定义的数据类型,是存储不同数据的集合体。
#include <iostream>
using namespace std;//结构体成员,数据类型的集合
struct Student
{string name;int age;int score;
};int main()
{//创建结构体变量 struct Student s1;//给结构体成员赋值 s1.name="kong";s1.age=18;s1.score=100; cout<< s1.name<<s1.age<<s1.score<<endl;system("pause"); return 0;}