当前位置:网站首页>小白大战指针 (上)
小白大战指针 (上)
2022-06-29 06:40:00 【Mertrix_ITCH】
一、初识指针
1.指针概述:
指针:内存中的一个地址。
指针变量:专门用来存放变量地址的变量。
1.1 定义 格式:类型说明 *变量名
eg:int *p
(类型说明:由指针变量所指向的数据类型决定。 *---表示该变量是一个指针变量)
1.2 赋值 格式:(& 变量名)
a.int a; int *p=&a [定义的同时进行赋值]
b.int a; int *p; p=&a; [先定义再赋值 !!!此时赋值时不加"*"]
使用指针变量前必须赋值,且给指针变量赋值只能赋予地址。
3.变量指针的引用 格式:* 指针变量
其含义是引用指针变量所指向的值。
#include<stdio.h>
int main()
{
int a,b;
printf("请对a、b赋初值:\n");
scanf("%d%d",&a,&b);
//int *p,*q; 定义指针变量p,q。
int *p=&a,*q=&b; //定义指针变量的同时进行赋值
//p=&a,q=&b; 为指针变量赋值
printf("a=%d,b=%d",*p,*q); //引用指针变量
return 0;
}
2."&" 和 “*”
&:取地址符,返回操作数地址的单目运算符;
*:指针运算符,返回指定的地址内的变量的值;
" &* “和 " * & " (”&“和”*"运算符的优先级相同,结合性:自右向左。)
以 int num , *p ; p = &num ; 为例
1. &*p = &(*p) =p 【取变量num地址】
2. *&num = *(&num)=num 【 取变量num的值】
#include<stdio.h>
int main() {
long i;
long *p;
printf("please input the number:\n");
scanf("%ld",&i);
p=&i;
printf("the r1 is:%d\n",&*p); /* 输出变量i的地址*/
printf("the r2 is:%d\n",&i); /* 输出变量i的地址*/
printf("the r3 is:%d\n",*&i); /* 输出变量i的值*/
printf("the r4 is:%d\n",i); /* 输出变量i的值*/
printf("the r5 is:%d\n",*p); /* 以指针形式输出变量i的值*/
}
3.指针自增自减运算
指针的自增自减运算是按照它所指向的数据类型的直接长度增减。
Example:
short i=4;
short *p=&i;
p++; //short-占2个字节,p的值增加2(2个字节)
#include<stdio.h>
int main(){
int i;
int *p;
printf("please input the number:\n");
scanf("%d",&i);
p=&i;
printf("the result1 is %d\n",p);
p++; //指向下一个存放int类型数的地址,p的值增加4(4个字节)
printf("the result2 is %d\n",p);
}
二、一维数组与指针
1.定义一个一维数组时,系统会在内存中为该数组分配储存空间,其数组名称就是该数组的首地址。
1.在定义一个指针变量,并将数组的首地址传给指针变量,则该指针就指向了这个一维数组 。
eg:int *p,a[5];
p=a || p&a[0]; //将数组a的首地址赋给p
2.通过指针引用一维数组
eg: *(p+i) || *(a+i)) //i:为循环变量,用循环语句移动指针打印数组元素
优化: printf("%d\t",*p++) //表示指针移动可以使用"++"或"--"运算符
#include<stdio.h>
int main() {
int *p,*q,a[5],b[5];
p=&a[0];
q=b;
printf("please input array a:\n");
for(int i=0; i<5; i++) {
scanf("%d",&a[i]);
}
printf("please input array b:\n");
for(int i=0; i<5; i++) {
scanf("%d",&b[i]);
}
printf("array a is:\n");
for(int i=0; i<5; i++)
printf("%-5d",*p++); //用p++控制指针移动
printf("\n");
printf("array b is:\n");
for(int i=0; i<5; i++)
printf("%5d",*(b+i)); //输出数组元素,*(q+i) 或 *(b+i)
printf("\n");
}
案例优化:
数组赋值时:采用(p++)进入下一存放基本整型数的地址。
@!!!若此时输出采用*p++,则重置指针变量p指向数组a的首地址。
Example:for(int i=1;i<5;i++)
scanf("%d", p++ || &a[i]);
#include<stdio.h>
int main(){
int *p,*q,a[5],b[5];
p=&a[0];
q=b;
printf("please input array a:\n");
for(int i=0; i<5; i++) {
scanf("%d",p++); //p++:指向下一存放基本整型数的地址。
}
printf("please input array b:\n");
for(int i=0; i<5; i++) {
scanf("%d",q++);
}
q=a; /*@此时必须:将指针变量p、q重新定位到数组a、b的起始位置*/
q=b;
printf("array a is:\n");
for(int i=0; i<5; i++)
printf("%-5d",*p++); //用*p++控制指针移动
printf("\n");
printf("array b is:\n");
for(int i=0; i<5; i++)
printf("%5d",*q++); //输出数组元素,*(q+i) 或 *(b+i)
printf("\n");
}

边栏推荐
- Markdown skill tree (1): introduction to markdown
- Postman pre request
- Viewing application and installation of Hana database license
- Blue Bridge Cup -- Analysis of the second batch of test questions of the 13th session
- 部署Prometheus-server服务 system管理
- 719. 找出第 K 小的数对距离(二分)
- Concurrent idempotent anti shake
- 精选西门子PLC工程实例源码【共300套】
- golang 修改 结构体切片的值
- SYSTEMd management node exporter
猜你喜欢

1183: patient queue

Appium自动化测试基础 — ADB常用命令(三)

Markdown skill tree (5): picture

Alicloud access resource: nosuchkey

KingbaseES V8R6集群维护案例之--单实例数据迁移到集群案例

【工控老马】单片机与西门子S7-200通信原理详解

Some examples.

systemd 管理node-exporter

Kyushu cloud helps Inner Mongolia's "counting from the east to the west" project to drive the smart new ecology of the surveying and mapping industry

Detailed design of PLC program control system for washing machine
随机推荐
关于开发web场景下如何解决手机访问web跨域问题
Schnuka: 3D visual inspection scheme 3D visual inspection application industry
游标长时间open导致表无法vacuum问题
并发幂等性防抖
KingbaseES 中select distinct on 语句
Deploy Prometheus server service system management
AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放
Wechat applet learning notes (summer vacation)
Gateway controller communication protocol
施努卡:3d机器视觉检测系统 3d视觉检测应用行业
tf.compat.v1.global_variables
KingbaseES应对表年龄增长过快导致事务回卷
面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?
TF. Repeat and stack operations of slim
手把手系列---安装SpotBugs、并快速上手使用
Explain canfd message and format in AUTOSAR arxml in detail
How to view software testing training? Do you need training?
About the problem that the kingbasees temporary file is too large
编译原理王者之路
SQL 注入绕过(六)