当前位置:网站首页>指针数组和数组指针有什么区别?
指针数组和数组指针有什么区别?
2022-07-19 05:04:00 【This is iNEvitable】
你好,很高兴您能读到这篇关于指针的博客,博客内容我将会以最简单的方式带您了解指针数组和数组指针的区别,那么开始正题。
指针数组,重点在于数组。例如我们常用的整形数组,字符数组等,它们的名字前半部分是存放数据的类型,后半部分是数组,所以,指针数组就是存放指针的数组。
我们看一下下面的小代码:
#include <stdio.h> int main() { int a = 0; int b = 0; int* arr[2] = {&a,&b}; return 0; }
arr数组的元素是两个地址,两个地址在数组所开辟的空间内存放,所以需要一个整形指针的类型接收,在C语言操作符的优先级中定义:[]数组操作符 比 *指针操作符优先级高,根据结合性,[]与arr结合表明arr是一个数组,类型是int*。
我们再看一下下面的小代码:
#include <stdio.h> int main() { char str1[] = "hello world."; char str2[] = "hello world."; const char *str3 = "hello world."; const char *str4 = "hello world."; if(str1 ==str2) printf("str1 and str2 are same\n"); else printf("str1 and str2 are not same\n"); if(str3 ==str4) printf("str3 and str4 are same\n"); else printf("str3 and str4 are not same\n"); return 0; }
答案是:not与are
我们知道数组在内存中是分配一块区域,地址是随机的,数组的元素从下标0开始所在的地址按由低到高的地址顺序存放,str1数组一块区域,str2数组一块区域,自然而然的把每个元素按顺序放在这个地址空间内,所以他们的地址是不同的。
str1与str2数组名表示首元素地址,记住除了&数组名与sizeof(数组名)之外都表示首元素地址,这两个操作符与数组名结合使用,数组名此时表示整个数组,&后+1跳过整个数组的字节,比如数组有10个元素,每个元素是int类型,*(&数组名+1)跳过40个字节.
字符指针变量初始化时只能存放一个指针类型(4/8)的字节,字符串"abcdef"表达式相等于首元素地址,a是一个字符,字符是个常量不能够被改变,a的地址存放在指针变量str3和str4中,所以str3与str4相等。
数组指针,重点在指针,意思为指向数组的指针,顾名思义,取出一个数组的地址,用数组指针的形式来接收
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int (*arr)[10] = &arr;
return 0;
}
此时()操作符优先级最高,*与arr结合表示它是一个指针,指针类型是int [10],说明它是一个存放数组的指针,名为数组指针。
感谢您的阅读,若有所帮助,期待您点赞、分享这篇博客。
边栏推荐
- 分享本周所学——Transformer模型详解
- Share what you learned this week - detailed explanation of transformer model
- pytorch 目标检测竞赛(一)数据分析
- 巧解杨氏矩阵
- Overlay number
- Pytorch Huawei cloud cup garbage classification summary (target detection)
- 自定义类型:结构体,位段,枚举,联合
- 嵌入式软件工程师
- The pit that wechat payment stepped on in those years
- 第七十五篇:学术论文写作技巧
猜你喜欢
基于STM32F103,用蜂鸣器播放歌曲
2021水下声学目标检测总结-Rank2
【YOLOv5实现玩手机检测】
区间覆盖问题
Share what you learned this week - detailed explanation of transformer model
Yolov5 détection des oiseaux
Basic introduction to multithreading (with sample code)
递归回溯—走迷宫
动态内存管理
The project is lost after Jenkins restarts (including the download address of Jenkins plugins)
随机推荐
Realization of ADC function based on stm32f030
练习题(1)创建一个集合c1,存放元素“one“,“two“,“three“
STM32-定时器
巩固复习之字符指针、数组指针、指针数组、函数指针
基数排序(桶排序)
Zabbix Server Ping链路监控,状态改变后通过邮件告警
Linear structure understanding
Pytorch uses free GPU test training (aistudio) yolov4 as an example
剑指offer 序列化二叉树
区间覆盖问题
STM32-使用定时器做延时函数时遇到的坑
Partition of integers
【学习笔记】Solid Works 作业记录
ThreadLocal learning notes
Securityerror: (:) [] occurs when CMD executes the command, parentcontainserrorrecordexception
Zabbix-agent 增加自定义监控项-- Ping 到目的地 IP 链路监控
yolov3的GUI界面(简易,图片检测)
华录杯-江苏违法广告检测-冠军总结
牛客剑指offer 机器人的运动范围
YOLO系列目标检测数据集大全