1.声明创建
//指针格式:类型 * 指针名; *引用地址符
int *point; //整型指针。存储的是内存地址
int number = 9; //存储的是值
point = &number; //数据类型要统一。指针point存储变量number的内存地址;&取地址符
number = 98; //修改变量值
printf("%d\n", *point); //98
*point = 56; //修改内存地址上的值
printf("%d\n", number); //56
void changeV(int *); //声明参数为整型指针的自定义函数
changeV(&number); //传入变量的地址
printf("%d\n", number); //87
//int * 表示声明一个整型指针的变量,这个指针变量名是p1
int *p1;
//*p1,表示引用变量地址,即普通变量,保存的是一个常量值
//*p1 = &number; //错误! //此处的*意为引用内存地址
//*p1 = 122; //原本这一步是正确的,但仅声明了指针却没有初始化一个内存地址,现在是不能对其赋值的
printf("%d\n", *p1); //87
//声明并初始化一个整型指针变量p2,其保存的 内存地址是变量number的
int *p2 = &number; //此处的*用来配合int声明
//p2是指针名,指针只能保存内存地址
//p2 = 988; //错误!
p2 = &number; //正确
printf("%d\n", *p2); //87
2.指向指针的指针
int n = 30;
int *pi = &n; //一级指针
int **pii = π //二级指针
int ***piii = &pii; //三级指针
printf("%d\n", ***piii); //30
printf("%d, %d \n",(pi == &n), (*pi == n)); //1真,1真
printf("%d, %d \n",(pii == n), (pii == &n)); //0假,0假
printf("%d, %d \n",(*pii == n), (*pii == &n)); //0假,1真
printf("%d, %d \n",(**pii == n), (**pii == &n)); //1真,0假
printf("%d, %d, %d \n",(pii == &pi), (pii == *pi), (pii == pi)); //1真,1真,0假
printf("%d, %d, %d \n",(*pii == &pi), (*pii == *pi), (*pii == pi)); //0假,0假,1真
printf("%d, %d, %d \n",(**pii == &pi),(**pii == *pi),(**pii == pi)); //0假,1真,0假
printf("%d, %d \n",(piii == n), (piii == &n)); //0假,0假
printf("%d, %d \n",(*piii == n), (*piii == &n)); //0假,0假
printf("%d, %d \n",(**piii == n), (**piii == &n)); //0假,1真
printf("%d, %d \n",(***piii == n), (***piii == &n)); //1真,0假
printf("%d, %d, %d \n",(piii == &pi), (piii == *pi), (piii == pi)); //0假,0假,0假
printf("%d, %d, %d \n",(*piii == &pi), (*piii == *pi), (*piii == pi)); //1真,0假,0假
printf("%d, %d, %d \n",(**piii == &pi), (**piii == *pi), (**piii == pi)); //0假,0假,1真
printf("%d, %d, %d \n",(***piii == &pi),(***piii == *pi),(***piii == pi)); //0假,1真,0假
printf("%d, %d, %d, %d \n",(piii == &pii), (piii == *pii), (piii == **pii), (piii == pii)); //1真,0假,0假,0假
printf("%d, %d, %d, %d \n",(*piii == &pii), (*piii == *pii), (*piii == **pii), (*piii == pii)); //0假,0假,0假,1真
printf("%d, %d, %d, %d \n",(**piii == &pii), (**piii == *pii), (**piii == **pii), (**piii == pii)); //0假,1真,0假,0假
printf("%d, %d, %d, %d \n",(***piii == &pii),(***piii == *pii),(***piii == **pii),(***piii == pii)); //0假,0假,1真,0假
3.指针与数组/字符串
#include <stdio.h>
void main() {
/*指针的类型,决定了饮用时读取的内存长度*/
int *p; //整型指针,读取时长度为4个字节
char c = 'A'; //字符,占内存1个字节
p = &c; //整型指针保存一个字符的地址
printf("%d\n", *p); //结果错误,因为多读取了3个字节的内存
char *pc; //字符指针
pc = &c; //字符指针保存字符地址
printf("%d\n", *pc); //正确,得到字符A的ASCII码65
//数组名就是数组首元素地址,即数组地址
int arr[] = {453, 3, 23, 549, 86, 943};
int i, *pr = &arr; //指向数组的首元素地址
for(i = 0; i < sizeof(arr)/sizeof(int); i++){
//pr是int类型的指针,pr+1 == 原地址 + 一个int单位的内存(4个字节)
printf("arr[%d] = %d\n", i, *(pr + i)); //+1,即到了下一个元素地址
printf("arr[%d] = %d\n", i, pr[i]); //等同于arr[i]。arr表示数组地址,指针pr也是
}
void changeArr(int *);
int arrs[] = {54, 74, 3, 9, 23, 8};
changeArr(arrs); //传入数组地址
printf("ARR2:%d, %d\n", *arrs, arrs[0]); //ARR2:88, 88
//字符串变量,可以修改
char str[] = "a string";
str[1] = 'A';
printf("%s \n", str); //aAstring
//字符串常量,初始化后不可更改
char *string = "point";
//string[2] = 'A'; //错误!
printf("%c \n", *string); //p。指向首字符
printf("%s \n", string); //point
printf("%p \n", string); //0040302F
//常量常驻内存,初始化相同值时,调用同一个内存地址
char *string2 = "point"; //常量,和上一个相同
printf("%p \n", string2); //0040302F
//字符串数组(二维数组)
char strArr1[][10] = {"abdsaf", {'V', 'G', 'B'}, "dsd", "bdfg"};
strArr1[0][3] = 'H'; //可以修改
printf("%s \n", strArr1); //strArr1指向第一个元素,strArr1[1]指向第二个
//字符串数组(指针数组)。常量数组,不可修改
char *strArr2[] = {"abdsaf", "sdfew", "dsd", "bdfg"};
printf("%s \n", *strArr2); //*strArr2指向第一个元素,*strArr2[1]指向第二个
char name[30];
//输入字符串
scanf("%s", name); //从name这个首地址挨个存储输入的字符,结尾自动加'\0'
printf("%s\n", name);//从name这个首地址挨个读取字符,直到'\0'
}
void changeArr(int *arr){ //arr接收数组地址
printf("ARR1:%d, %d\n", *arr, arr[0]); //ARR1:54, 54
*arr = 88; //改变索引为0的元素的值
}
4.指针与函数
#include <stdio.h>
char *funcP(); //返回指针的函数
void funcForP1();
void funcForP2();
void main() {
//调用返回指针的函数
printf("%s\n", funcP());
//指向函数的指针。void是函数返回值,(*指针名)是固定写法,()是函数参数
void(*p)(int) = funcForP1;
(*p)(0); //通过指针引用地址调用函数
p = funcForP2;
p(222); //通过指针调用函数
}
//返回指针的函数
char *funcP(){
return "return str";
}
void funcForP1(int i){
printf("指向函数的指针调用111\n");
}
void funcForP2(int i){
printf("指向函数的指针调用%d\n", i);
}
- end