传数组名的小问题
来源:互联网 发布:威尔取模软件 编辑:程序博客网 时间:2024/04/30 18:57
有人问到传数组名,和数组名引用的问题,平时没怎么使用这么怪异的语法
没有想明白;后来在水木清华上咨询到了清晰的解答,现深入总结如下:
例子:
#include <iostream>
#include <typeinfo>
using namespace std;
void fArray(int *a, int b[], int (*c)[4], int (&ra)[4], int d[4])
{
cout << "In fArray(...):" << endl;
cout << "int* a type " << typeid(a).name() << endl;
cout << "int b[] type " << typeid(b).name() << endl;
cout << "int (*c)[4] type " << typeid(c).name() << endl;
cout << "int (&ra)[4] type " << typeid(ra).name() << endl;
cout << "int d[4] type " << typeid(d).name() << endl;
return;
}
int main()
{
int a[4] = {1,2,3,4};
int (&ra)[4] = a;
cout << "In main():" << endl;
cout << "int a[4] type " << typeid(a).name() << endl;
cout << "int (&ra)[4] type " << typeid(ra).name() << endl;
fArray(a,a,&a,a,a);
return 0;
}
g++ 4.4.1结果:
In main():
int a[4] type A4_i
int (&ra)[4] type A4_i
In fArray(...):
int* a type Pi
int b[] type Pi
int (*c)[4] type PA4_i
int (&ra)[4] type A4_i
int d[4] type Pi
main()里面a ra都是int[4]类型
而作为形参int(&ra)[4],int d[4]怎么类型就不一致呢,是否语义不一致?
水木清华jasss回答如下,仔细重看了标准相关内容,确实如此。
Because we have a standard array-to-pointer
conversion here, and here the argument
int d[4]
is same as
int *d
, and the array size 4 is meaningless here.
For int (&ra)[4], here ra is a reference to
an array with 4 int element, array a is <br /><ul><li></li></ul><br /><br /><div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=187432ac-8829-830d-adaf-928cb9997ed7" /></div>
bound to ra directly.
: 这不会导致int a[4]和int d[4]存在语义不一致吗?
The two function arguments are different...
For ra, the size information is part of the type
and d is merely a pointer.
For example, changing arguemnt ra to "int (&ra)[5]"
then the program won't compile, while the compiler
does not care even you replacing "int d[4]" with
"int d[5]".
- 传数组名的小问题
- 数组名及数组名的地址(还有一个小问题没解决)
- 数组的数组名的问题
- 直接输出数组名的问题
- 数组名取地址带来的问题
- 数组名表示指针常量的问题
- 数组的两个小问题
- 数组名退化问题
- 数组名问题
- 有关sizeof 数组名的问题 数组名和指针的深入理解(C++)
- 数组名、数组名取地址以及作为函数传递带来的问题
- 数组名与数组名的地址
- 数组名与数组名的地址
- ado.net操作时 表名冲突的小问题
- 关于二维数组名和指针的指针的问题
- c++输出数组名问题
- 关于对数组名取地址的问题
- 关于一维和二维数组名是指针的问题
- 办公软件常识 (转)
- java反射与CGLIB代理的一个细节。
- 《C现代方法》第二章:C语言基本概念 (2)
- 数据库设计三大范式应用实例剖析
- 郁闷的心情
- 传数组名的小问题
- 迟到的TOP
- OOA/OOD/OOP
- 智力题
- 搭建Android开发环境
- 循环例子-for高级应用
- 处事22计、心态决定你的人生(每天必读)、伤心时要读的50句话和人的基本礼仪(保证你能学到不少东西)
- ASP中有关双引号,单引号以及&号的解释
- android框架