1.const
int sloth =3 ;
const int* ps; //一个指向const int的指针:指针指向一个常量对象, 可防止使用指针来修改所指向的值。*ps的值为const,不能被修改
int* const finger = &sloth; //一个const指针,指向int: 指针本身声明为常量,可防止改变指针指向的位置。
1. 将常规变量的地址赋给常规指针
2. 将常规变量的地址赋给指向const指针
那么还有
将const变量的地址赋给指向const的指针
将const的地址赋给常规指针,这样是不允许的,因为若可以使用,那么将可以使用指针来改变const的变量
当有二级间接关系,
const int **pp2;
int *p1;
const int n = 13;
pp2 = &p1; //这样是不允许的,若这样可以,
*pp2 = &n;
*p1 = 10; //那么n的值将被改变
不能将const指针赋给非const指针
const int months[12] = {3}
int sum(int arr[], int n)
int j = sum(months, 12); //这样不允许
指向const对象的const指针:
double trouble = 2.0E30
const double* const stick = &trouble;
2.二维数组
int data[3][4] = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4}}; //有3个元素,每个元素本身是一个数组,有4个int值组成。
int sum(int (*ar2)[4], int size);
int *ar2[4];//一个数组,里面有4个指向int类型的指针
int (*ar2)[4]; //一个指向由4个int组成的数组的指针。
比如: sum(data, 3)
int sum(int (*ar2)[4], int size)
{
int total = 0;
for(int r = 0; r <size;r++)
for(int c = 0; c <4;c++)
total += ar2[r][c];
return total;
}
ar2[r][c] = *(*(ar + r) + c);
3.c-风格字符串
c-风格字符串与字符串数组的区别:
前者有内置的结束符'\0'。
4. string对象
string list[5];
getline(cin, list[0]);
cout<<list[0];
5. 函数地址
1. 声明函数指针
double pam(int);//函数原型
double (*pf)(int); //函数指针
double *pf(int); //返回double类型指针的函数
<=> double* pf(int);
double pam(int);
double (*pf) (int);
pf = pam;
double x = pam(4);
这两者都行:
double y = (*pf)(5);
double y = pf(5);
double (*pfArray[3])(double a, double b); //一个指针数组,每个指针指向一个函数