有关引用的几个问题?

kingzeus 2002-02-08 03:23:17
1 引用与匿名联合有什么区别?
union { int a,b;} 与 int a,&b=a;
a,b的值与地址都是一样的,这两句有什么区别?

2 是否存在对数组的引用?
记得,以前学C++时,书上说,若一个变量声明为T&, 它必须用T类型的变量或对象或能转化为T的对象初始化。数组只是某个数据类型元素的集合,不是一个数据类型,所以,不存在对数组的引用。
int a[5];
int &ra[5]=a; //error
但是,数组名表示该集合空间的起始地址, int* &ra=a; 对ra可进行数组操作。不过,这好象是指针的引用,不是数组的引用。
最近,看过的一本书上说,存在数组的引用。那么,数组的引用到底有没有??
...全文
14 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sam1111 2002-02-08
  • 打赏
  • 举报
回复
楼上的说的对。
对数组的引用实际上是对指针的引用。
hz129 2002-02-08
  • 打赏
  • 举报
回复
1.
union {
int a;
float b;
} x;
x.a and x.b use the same address, but only one of them can be used at a time. if you write:
x.a = 3;
float f = x.b;
you'll found you got an error result!

int a;
int &b = a;
a and b is the same integer but two different names. when you changes value of a, the value of b is changed also.

2. int *&ra = a;
there ra is a reference to a pointer. you can't define a reference to an array directly.

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧