目录
一、指针
1.1 内存地址
计算机中的存储地址是以字节为单位的一片连续的存储空间。定义变量时则会根据数据类型随机分配一块空间给变量使用,这个空间有一个随机的地址值,用16进制表示(例:int a =10; 开辟了一个字节大小为4的空间a,&a是取这个空间的地址值)。
1.2 指针概述
指针是一种数据类型(地址构造数据类型),空间大小为4个字节(不论指针类型是多少)。一般用来保存地址,通过指针可以访问保存的地址中的数据。指针的本质是地址
1.3 指针符号
&:取出指针中存储的地址
*:取出指针存储地址中的数据内容
二、指针应用
2.1 指针变量
2.1.1 定义
格式:指针类型 *变量名 int *a、char *b
指针类型:代表指针存储的地址所指向的数据的类型
注:①定义指针时若暂时用不到则将指针指向空,不然会指针会指向随机的地址值,可能会影响程序运行。 int *a = NULL
② 定义指针时的*仅代表定义的是指针变量,无其他特殊含义
③ 指针所指向的地址可以中途更改
④ 指针变量只能进行+、-、++、--运算
⑤ 一个变量可以被多个指针同时指向,一个指针只能指向一个变量
2.1.2 应用
#include <stdio.h>
int main()
{
int a = 10,b = 20,c = 30;
int * p = &a,*w = 0,*r = NULL;
w= &a;
r= &a;
a = 12;
printf("%d\n",*p); //12
printf("%d\n",*w); //12
printf("%d\n",*r); //12
*w = 24;
printf("%d\n",a); //24
printf("%d\n",*p); //24
printf("%d\n",*w); //24
printf("%d\n",*r); //24
r = &b;
r = &c;
printf("%d\n",*r); //30
}
2.2 指向数组的指针
指针和数组一样,都可以通过变量的下标来访问数组中的各个元素。指针变量+1,指针内存放的地址+n(n由指针指向空间类型决定)。使用“++”运算符来访问数组中的下一个元素,使用“--”运算符来访问数组中的上一个元素。
#include <stdio.h>
int main()
{
int a[5] = {12,15,26,13,14};//a/&a[0]
int *p = a;
int i;
for(i = 0 ; i < 5 ; i++)
{
printf("%d\n",p[i]);
}
}
2.3 指向字符串的指针
#include <stdio.h>
#include <string.h>
int main()
{
char buff[128] = "123456";
char *p = buff,*q = NULL;
p = "789456";
q = "456qwre";
printf("%s\n",q);
printf("%c\n",*(q+1));
printf("%s\n",p);
printf("%s\n",buff);
}
2.4 指向指针的指针
指向指针的指针也被称作二级指针 int **p
① 二级指针(p)是指向一级指针的地址(二级指针保存它指向的一级指针的地址)。
② *二级指针(*p)是取一级指针指向的变量的地址(取得是一级指针保存的地址)。
③ *二级指针(**p)的内容是取一级指针指向的变量地址中的内容(取得是数值)
#include <stdio.h>
int main()
{
int a = 10;
int *p = NULL;
int **q = NULL;
p = &a;
q = &p;
printf("%p\n",&a);
printf("%p\n",p);
printf("%p\n",q);
printf("%p\n",&p);
printf("%p\n",*q);
**q = 20;
printf("%d\n",**q);
printf("%d\n",a);
printf("%d\n",*p);
}
2.5 指针数组和数组指针
2.5.1 指针数组
指针数组本质上是一个数组,数组中的元素都是指针类型
#include <stdio.h>
int main()
{
char *buff[] = {"123","456","789"};
// 本质是数组,数组类型(每个元素)是指针类型
printf("%s\n",*buff);
printf("%s\n",*(buff+1));
printf("%s\n",*(buff+2));
printf("%s\n",buff[0]);
printf("%c\n",*buff[0]);
printf("%c\n",*(buff[0]+1));
printf("%c\n",*(buff[1]+1));
}
2.5.2 数组指针
数组指针本质是一个指针,一般用于二维数组。
int (*p)[5] :定义了一个列长度为5的数组指针
#include <stdio.h>
int main()
{
int num[2][3] = {
1, 2, 3 ,
4, 5, 6
}; //定义一个二维数组
int(*pnum)[3] = NULL; //定义数组指针
pnum = num;//数组指针指向二维数组
printf("数组类型大小%d\n",sizeof(int [2][3]));
printf("数组类型大小%d\n",sizeof(num));
printf("num[0][0]=%d\n", num[0][0]);
printf("num[1][0]=%d\n", num[1][0]);
//指针打印
printf("num[0][0]=%d\n",(*pnum)[0]);
printf("num[0][1]=%d\n",(*pnum)[1]);
printf("num[1][0]=%d\n", (*(pnum + 1))[2]);
}
2.6 指针函数和函数指针
2.6.1 指针函数
指针函数的本质上是一个函数(函数的类型是指针类型)
地址的返回只能返回静态变量的首地址、动态开辟(malloc)的地址,全局变量的地址等,不能返回局部变量的地址
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * fun(void)
{
char *p = NULL;//32位,4个字节 ,保存地址
p = (char *)malloc(128);//在堆区申请128字节空间
if(p == NULL)
{
printf("申请空间失败\n");
}
else
{
printf("申请空间成功\n");
}
if(strcpy(p,"123456789") != NULL)
{
printf("ok\n");
}
else
{
printf("no\n");
}
return p;
}
int main()
{
char *p =NULL;
p = fun();
printf("fun:%s\n",p);
}
2.6.2 函数指针
函数指针本质上是指针,指针指向的类型是一个函数类型,与指针用法相同。
#include <stdio.h>
int fun(int a,int b);
int main()
{
int (*p)(int,int) = NULL;
int a;
p = fun;
a = fun(1,2);
printf("%d\n",a);
a = p(1,2);
printf("%d\n",a);
}
int fun(int a,int b)
{
return a+b;
}