当前位置:网站首页>(c语言)数组是一种特殊的指针?
(c语言)数组是一种特殊的指针?
2022-07-22 09:13:00 【现无忘之思】
C语言学习时,学到数组与指针的联系时,我们讲到数组是一种特殊的指针,特殊在哪呢?
数组在定义时,如int a[10],即开辟了特定的内存空间来存放数组中的元素,数组起到一个容器的作用,可通过访问容器的地址给容器内的东西赋值或修改,容器本身的地址是不可修改的,即理解为int* const p=a。
但要注意,上式中的a(数组名)可作为数组a中的首元素地址使用,那么&a又表示何种意义?看以下代码
int a[10];
int* p=NULL;
p=a;
printf("%d %d %d\n",sizeof(a),sizeof(&a),sizeof(p));
printf("%d %d %d %d",a,&a,&a+1,a+1);
输出结果为:
40 8 8
6487536 6487536 6487576 6487540
--------------------------------
Process exited after 0.03779 seconds with return value 31
请按任意键继续. . .
//64位编译器
40 4 4
6422132 6422132 6422172 6422136
--------------------------------
Process exited after 0.104 seconds with return value 31
请按任意键继续. . .
//32为编译器
如何解释a与&a的区别?
a既表示数组名,又有首元素地址的含义;&a则是数组a的首地址;第一行输出40 8 8, 40为整个数组的字节长度,后续的两个8都是指针(地址)的字节长度,在64位编译器下应为8, 32位则为4;
第二行输出作的是指针的加减法;分析:age==&a[0],只表示数组首元素的地址,&age 则认为是取了整个数组的首地址,这两个地址是相等的。但地址作加减运算时,其是有单位的,( 胡凡 曾磊的《算法笔记》中描述:指针作加减法,其指向的基类型为单位,计算结果为单纯的字节地址运算后乘上或除以其单位的字节), a+1的单位(a替换成&a[0]来看)是一个int字节的长度,&age+1的单位是一个数组的长度 。
由此可见,数组和指针有联系又有区别,数组有确定的连续的内存空间分配,而指针变量只是指向某一地址的变量。
边栏推荐
- call()和apply()
- Toss Phoenix system (by quqi99)
- Try kolla-ansible (by quqi99)
- ECSHOP prompts "the template file themesmobile/mo_paleng_mobile/index.dwt cannot be modified"?
- ecshop禁用缓存、关闭缓存
- Js高级-词法作用域
- Summary 20220119
- App mobile terminal test [6] application program (APK) package management and activity
- App移动端测试【6】应用程序(apk)包管理与activity
- 华为手机锁定应用
猜你喜欢
TCP与UDP及三次握手,四次挥手
PHP开发中csrf攻击的简单演示和防范
uniapp 微信小程序地图,在高德app、腾讯app、百度app、苹果地图app 中打开
C语言简易TCP服务端程序
Uniapp wechat applet map, open in Gaode app, Tencent app, baidu app, apple map app
ecshop配置微信支付,微信开发者工具微信支付时弹窗提示“未绑定网页开发者”问题
【FatFs】FAT32文件系统协议总结(理论+实践)
04.接口隔离原则(Interface Segregation Principle)
C language simple TCP server program
[audio] analysis and summary of PCM audio data transmitted by I2S (II)
随机推荐
03.单一职责原则(Simple Responsibility Pinciple)
Summary 20220213 (Floyd and Dijkstra)
Wechat official account web page authorization ----- redirect_ The URI domain name is inconsistent with the background configuration, and the error code is 10003
C language simple TCP server program
05.迪米特原则(Law of Demeter LoD)
ecshop禁用缓存、关闭缓存
Fortress machine, DMZ area
Go 并发模式:管道和取消
App移动端测试【6】应用程序(apk)包管理与activity
Summary 20220208 (KMP)
使用工厂的方法创建对象
Summary 20220119
【FatFs】FAT32文件系统协议总结(理论+实践)
TCP与UDP及三次握手,四次挥手
ECSHOP prompts "the template file themesmobile/mo_paleng_mobile/index.dwt cannot be modified"?
The routing interface of local access to local TP5 of wechat applet is normal. Why can't you scan the code on the mobile phone to preview and get data?
【STM32】STM32 SDIO SD卡读写测试(二)-- SD_Init之Power On阶段
总结20220119
[SDIO] sd2.0 protocol analysis summary (I) -- Introduction to SD card basic probability
OSI model, tcp/ip model