当前位置:网站首页>一级指针&二级指针知识点梳理
一级指针&二级指针知识点梳理
2022-06-12 21:01:00 【又秃又弱】
普通指针(一级):
定义:
指针=地址
取地址符:&+变量名(获取该变量的地址) ,针对所有变量都适用
输出地址:printf(“%d”,&a); //%d 输出10进制 ,%p输出16进制
补充:scanf里scanf("%d",&a);用&输入值也是一样的道理,通过地址才能在scanf函数中形参中把值带过来
使用:
(1)通过解引用修改变量的值:
*+指针变量:解引用,间接访问符
例如:int* p=&a;
*p=100;

p指向a,p格子内是a的地址。
*p是对a解引用,得到a格子内的值,也就是a的值,令*p=100,则a=100
指针变量和普通变量的异同点:
相同点:
- 都能改变
- 都有地址
不同点:
- 指针变量可以解引用,普通变量不可以
总结:
int p;//定义整型变量
int* p; //定义整形地址变量
&a; //整形地址变量
p=&a;//整形地址变量p保存整形a地址值
int*p=&a;//把上面两步合并
指针字节大小:
与类型无关,由平台决定
X86:32位操作系统;2^32 一个字节8位,这种情况下,指针4个字节
x64:64位操作系统 2^68 这种情况下,指针8个字节
一字节=-128到127 256 2^8
1TB=1024GB;
1GB=1024MB;
1MB=1024KB;
1KB=1024B;
1B=8b;
补充:
(1)Int*p1,p2,p2;
p1:指针变量,p2,p3:整型变量
(2)typedef int* INT;
INT p1,p2,p3; //此时,三个均为指针变量,等价于Int*p1,*p2,*p2;
(3)#define INT int*
INT p1,p2,p3; //此时,仅第一个为指针变量,等价于Int*p1,p2,p2;
习题练习:
eg1:
//知识点:指针类型必须严格相等才能赋值(就是变量什么类型,指针什么类型)
char a;
short b;
int c;
unsigned long d;
double e;
float f;
(1)定义指针p1,保存a的地址
char* p1 = &a;
(2)定义指针p2,保存b的地址
short* p2 = &b;
(3)定义指针p3,保存c的地址
int* p2 = &c;
(4)定义指针p4,保存d的地址
unsigned long* p2 = &d;
(5)定义指针p5,保存e的地址
double* p5 = &e;
(6)定义指针p6,保存f的地址
float* p6 = &f;
eg2:
int a, b, c;
int* p1 = &a;
int* p2 = &b;
int* p3 = &c;
(1)通过p1,将a的值改为20,并输出;
*p1 = 20;
printf("%d", *p1);
(2)通过p2,将b的值改为30,并输出;
*p2 = 30;
printf("%d", *p2);
(3)通过p1,p2,p3,将c的值改为a×b,并输出;
*p3 = *p2 * *p2;
printf("%d", *p3);
(4)定义指针p,指向a
int* p = &a;
(5)通过p1,将a的值改为10;
*p1=10;
(6)定义指针p,将p指向b,并通过p将b的值改为20
int*p = &b;
*p = 20;
(8)定义指针p,通过p输出a,b的值(需要多条语句)
int*p;
p = &a; printf("%d", *p);//输出a的地址;
p = &b; printf("%d", *p);//输出b的地址;
指针的好处:
(1)一般变量想要带回值,只能用return带回一个值,而指针可以通过形参带回多个值。
例:
void MyScanf(int* a, int* b)
{
*a = 1;
*b = 0;
}
int main()
{
int a ;
int b ;
MyScanf(& a, & b);
return 0;
}
例:
int Operate(int a,int b,int* sum,int* mul)
{
*sum=a+b;//主函数里把地址传过来,该函数内部对其解引用替换值
*mul=a*b;
}
int main()
{
int sum,mul;
Operate(1,2,&sum,& mul);
}
(2)因为形参与实参的地址不一样,形参是局部变量,无法通过形参直接修改实参值,需要地址改变,而指针可以通过调用函数改变其值。
例:
//交换变量
void Swap1(int *a,int* b)
{
int *temp=a;
a = b;
b = temp;
}
void Swap2(int *a,int* b)
{
int temp=*a;
*a = *b;
*b = temp;
}
//如下Swap函数错误:对野指针进行解引用
void Swap(int *a,int* b)
{
int *temp=*a;
*a = *b;
*b = *temp;
}
二级指针
举例说明:
例:int a=10;
int *p=&a; //p指向a,(p格子内)保存a的地址
int **pp=&p; //pp指向p,(pp格子内)保存p的地址

“*”:解引用(格子箭头往前指)
pp:pp格子=200;
*pp:p的值=100 //1个“*”,箭头往前走1下。*pp是对p解引用得到p格子内的值,也就是100;
**pp:a的值=10 //2个“*”,箭头往前走2下。**pp是对a解引用得到a格子内的值,也就是10;
用法:
改变pp的指向,使其指向q:pp=&q;
通过二级指针pp修改a的值:**pp=200;
总结:
一级指针关联变量的地址,
二级指针关联一级指针的地址。
补充:
&&a错误
*&a=a; 对的,对a取地址又对其解引用
&*a错误,不能对a解引用
例题分析
eg1:
int a = 10;
int b = 20;
int* p;
p = &a;
*p = 30;
int** pp = &p;
(1)a,和**p的值为多少?
a=30; //p指向a,*p = 30修改了a的值;
**pp = 30;//解两次引用,箭头跳两次指向a,a格子内值为30

eg2:
int a = 10;
int b = 20;
int* p= &a;
*p=100;
int** pp = &p;
*pp = &b;
**pp =1000;
int ***ppp =&pp;
***ppp=1;
(2)a和b的值为多少?
*p=100; 将a的值改为100
*pp = &b; 将p的指向修改为b
**pp =1000;解2次引用,将b的值修改为1000
int ***ppp =&pp; 定义ppp指向pp
***ppp=1;将b的值修改为1
所以,a=100,b=1

补充:对野指针的理解
int *p=&a
printf(“%d”,*p); //可以
但是
p=NULL;
printf(“%d”,*p);//不行,不能对空进行解引用
但是,如下情况:
定义Fun(p)函数,在内部对p置空,调用Fun(p),再输出printf(“%d”,*p)
void Fun(int* p)
{
p = NULL;
}
int main()
{
int a = 10;
int* p = &a;
Fun(p);
printf("d\n", *p);
return 0;
}
//此时发现Fun函数并没有起作用,因为:A函数调用B函数,如果需要通过修改A中实参的值,则必须传指针,在B中再解引用
//fun函数未被执行,想要通过另一个修改其值,必须传指针,函数内部解引用(加*)
//如下所示,会导致错误
void Fun(int** p)
{
**p = NULL;
}
总结:
NULL空指针,当前为无效指针,相当于告诉用户该指针作废
野指针:没有访问权限,指针定义时未初始化,易产生野指针
边栏推荐
- test
- JS深浅拷贝
- MySQL + PostgreSQL batch insert update insertorupdate
- EDI 855 purchase order confirmation
- SAP QM preliminary - cannot assign a sampling policy to an inspection characteristic when maintaining an inspection plan by executing transaction code qp02?
- Typescript definition type: type 'timeout' cannot be assigned to type 'number';
- Research Report on market supply and demand and strategy of hydraulic operating table industry in China
- Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot
- Preliminary understanding of regular expressions (regex)
- 多机房动环状态网络触摸屏监控解决方案
猜你喜欢

Solution of multi machine room dynamic loop status network touch screen monitoring

Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer

At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?

Introduction to scala basic grammar (III) various operators in Scala

Typescript definition type: type 'timeout' cannot be assigned to type 'number';

Composer version degradation

How do testers plan for their future? To achieve 25K in 2 years?

torch. nn. Linear() function

半路自学测试成功转行,第一份测试工作就拿10k

Leetcode: 210. Programme II
随机推荐
Large and small end conversion
Scope and scope chain
pytorch transforms. Use of lambda
Simplest ALV template
Restful API interface specification
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Image processing 12- image linear blending
Introduction to scala basic grammar (III) various operators in Scala
Compréhension préliminaire des expressions régulières cognitives (regex)
#113 Path Sum II
Let Google browser fofa plug-in come alive
Gather function in pytorch_
Research Report on market supply and demand and strategy of China's hydraulic hammer industry
Shell language
leetcode:207. 课程表
#141 Linked List Cycle
Data visualization - histogram
函数的了解
Teamwork collaboration application experience sharing | community essay solicitation
Minio client (MC command) implements data migration