当前位置:网站首页>c语言语法基础之——指针(字符、一维数组) 学习
c语言语法基础之——指针(字符、一维数组) 学习
2022-06-26 09:33:00 【坦桑尼亚奥杜威峡谷能人】
指针理解:
- 指针也是一个变量;只能装地址,不能装常量;
- 指针一般用p、q、m、n 来表示,不用b表示;
- 指针的类型与它指向的变量的类型一致;
- 指针运算符: * 、 & ( * 代表指针;& 代表地址 )
int 占内存2个b
float 占内存4个b
普通变量指针

int a=3,b=4,d;
int *b=&a; // * 代表指针;& 代表地址
int *b=5; // 会报错,因为只能装地址,不能装常量
printf("%d",a);
printf("%d",*b); //要用到a的值得话,用 *b ; b 是 a 的指针;b 是 a 的地址;*b 表示该指针指向的变量的值
printf("%d",b); //要用到a的地址得话,用 b ,内存地址是个十六进制值;计算机内存一般以16进制来表示。b 表示该指针指向的变量的地址
int c=*b+1; // ;
printf("%d",c);
p1=&a; // 不能是 *p1=&a ,会报错
p2=&b;
d=*p1+*p2;
p2=&a;
p=&(a+1); // 错误写法
打印出:
3
3
22316
4
// 定义指针的两种方式
int a, *p; // 一
p=&a;
int a, *p=&a; // 二
指针版最大数:
int main(int argc, char *argv[]) {
int a,*p=&a,i,max=0,*p1=&max;
for(i;i<10;i++){
scanf("%d",p);
if(*p>*p1){
p1=p;
}
}
printf("最大数是:%d",*p1);
return 0;
}
数组变量指针;
p指向谁的地址,此时就替代谁来操作;
注意点:
数组名本身是特殊的指针,是个常量,不能参加运算,比如:
int a[]={1,2} 、char a[]=“Hello W”
a --> 数组名本身,默认是数组第一个元素
指针p是个变量,比如:
*p=“Hello W”
int *p,i;
int a={
1,2,3,4,5};
p=a;// *p=a[0]=1
p++; // 2
p=a+4;
printf("%d", *p); // 5 主要看指针指向哪,移到哪就代表哪个地方
p--; // 4
main() {
// 斐波那契前12项之和 指针版
int *p,sum=2;
int a[12]; // 一般都是12个元素;10、11 也可以,但是9就不行了,因为下边有一句*p=*(p-1)+*(p-2),指针是可以动的,这里可以扩充两个元素
p=a;
*p=1;
printf("第一项是 %d",*p);
p++;
*p=1;
printf("第二项是 %d",*p);
p++; // 需要定位到从下标2开始,也就是第3项开始 ;或者for括号里写 p=a+2
for(;p<=(a+11);p++){
// p<(a+11) 也可以写成 p<=a+11
*p=*(p-1)+*(p-2);
printf("\n%d",*p); // a[i]
sum=sum+*p;
}
printf("\n斐波那契数列前12项和=%d",sum);
return 0;
}
指针与数组的关系:
二维数组一般用保留 i 的方式去处理;
一维数组用哪种方式都行。
边栏推荐
- kubernetes集群部署(v1.23.5)
- Catalogue gradué de revues scientifiques et technologiques de haute qualité dans le domaine de l'informatique
- Explained: A Style-Based Generator Architecture for GANs (StyleGAN)
- 我的创作纪念日
- thinkphp5使用composer安装插件提示php版本过高
- js---获取对象数组中key值相同的数据,得到一个新的数组
- Halcon photometric stereoscopic
- MapReduce&Yarn理论
- How to view the data mini map quickly and conveniently after importing data in origin
- php提取txt文本存储json数据中的域名
猜你喜欢

Jz2440--- using uboot burning program

logback

3 big questions! Redis cache exceptions and handling scheme summary

MapReduce&Yarn理论

【轨迹规划】Ruckig库的测试

【CVPR 2021】Unsupervised Pre-training for Person Re-identification(UPT)

There is a strong demand for enterprise level data integration services. How to find a breakthrough for optimization?

install opencv-contrib-dev to use aruco code

Thinkphp5 manual error reporting
![[Journal of Computer Aided Design & computer graphics] overview of research on pedestrian re recognition methods based on generated countermeasure network](/img/a9/1361df052f0474e5c50b948a92c42a.jpg)
[Journal of Computer Aided Design & computer graphics] overview of research on pedestrian re recognition methods based on generated countermeasure network
随机推荐
"One week to finish the model electricity" - 55 timer
mysql 数据库字段查询区分大小写设置
LeetCode 0710.黑名单中的随机数 - 预处理实现O(1)取值
Comprehensive interpretation! Use of generics in golang
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
jz2440---使用uboot燒錄程序
节流,防抖,new函数,柯里化
Spark based distributed parallel processing optimization strategy - Merrill Lynch data
"One week's work on Analog Electronics" - optocoupler and other components
欧冠比赛数据集(梅西不哭-离开巴萨也可能再创巅峰)
首期Techo Day腾讯技术开放日,628等你
深度学习(初识tensorflow2.版本)之三好学生成绩问题(1)
集合对象复制
Thinkphp5 using the composer installation plug-in prompts that the PHP version is too high
[Journal of Computer Aided Design & computer graphics] overview of research on pedestrian re recognition methods based on generated countermeasure network
How to view the data mini map quickly and conveniently after importing data in origin
SQL modification of table structure
【CVPR 2021】Joint Generative and Contrastive Learning for Unsupervised Person Re-identification
计算领域高质量科技期刊分级目录
Yolov5 results Txt visualization