前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >The Address of an Array

The Address of an Array

作者头像
青木
发布2018-05-28 15:47:18
5290
发布2018-05-28 15:47:18
举报
文章被收录于专栏:我是业余自学C/C++的

The name of the array is interpreted ad the address of the first element of an array,whereas applying the address operator yields the address of the whole array.

代码语言:javascript
复制
#include<iostream>
using namespace std;

int main(void)
{
	short tell[10];    //tell a array of 20 bytes
	cout<<sizeof(tell)<<endl; // the result of output is 20
	cout<<tell<<endl;    //displays &tell[0]
	cout<<&tell<<endl;    //displays address of whole array
	return 0;  
}

Numberically,these two addresses are the same,but conceptually &tell[0],and hence tell,is the address of a 2-byte block of memory,whereas &tell is the address of a 20-byte block of memory. So the expression tell+1 adds 2 to the address value,whereas &tell+1 adds 20 to the address value. tell is type pointer-to-short,or short ,and &tell is type pointer-to-array of 20-shorts or short ()[20].

代码语言:javascript
复制
short (*pas)[20] = &tell;    //pas pointer to array of 20 shorts

If you omit the parentheses,precedence rules would first associate [20] with pas,making pas an array of 20 pointers-to-short,so the parentheses are necessary.Next,if you wish to describe the type of a variable,you can use the declaration of that variable ad a guide and remove the variable name.Thus,the type of pas is short (*)[20].Also note that because pas is that set to &tell,*pas is equivalent to tell,so (*pas)[0] would be the first element of the tell array.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档