C++数组作为函数参数的几个问题
2014-02-13 14:58
375 查看
本文需要解决C++中关于数组的2个问题:
1. 数组作为函数参数,传值还是传址?
2. 函数参数中的数组元素个数能否确定?
先看下面的代码。
[cpp] view
plaincopy
#include <iostream>
using namespace std;
void testArrayArg(int a[])
{
cout << endl;
cout << "in func..." << endl;
cout << "array address: " << a << endl;
cout << "array size: " << sizeof(a) << endl;
cout << "array element count: " << sizeof(a) / sizeof(a[0]) << endl;
cout << "changing the 4th element's value to 10." << endl;
a[3] = 10;
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
cout << "in main..." << endl;
cout << "array address: " << a << endl;
cout << "array size: " << sizeof(a) << endl;
cout << "array element count: " << sizeof(a) / sizeof(a[0]) << endl;
testArrayArg(a);
cout << endl << "the 4th element's value: " << a[3] << endl;
return 0;
}
运行结果如下:
in main...
array address: 0012FF4C
array size: 20
array element count: 5
in func...
array address: 0012FF4C
array size: 4
array element count: 1
changing the 4th element's value to 10.
the 4th element's value: 10
当我们直接将数组a作为参数调用testArrayArg()时,实参与形参的地址均是0012FF4C。并且,在testArrayArg()中将a[3]的值修改为10后,返回main()函数中,a[3]的值也已经改变。这些都说明C++中数组作为函数参数是传址。
特别需要注意的是,在main()中,数组的大小是可以确定的。
array size: 20
array element count: 5
但作为函数参数传递后,其大小信息丢失,只剩下数组中第一个元素的信息。
array size: 4
array element count: 1
这是因为C++实际上是将数组作为指针来传递,而该指针指向数组的第一个元素。至于后面数组在哪里结束,C++的函数传递机制并不负责。
上面的特性可总结为,数组仅在定义其的域范围内可确定大小。
因此,如果在接受数组参数的函数中访问数组的各个元素,需在定义数组的域范围将数组大小作为另一辅助参数传递。则有另一函数定义如下:
[cpp] view
plaincopy
void testArrayArg2(int a[], int arrayLength)
{
cout << endl << "the last element in array is: " << a[arrayLength - 1] << endl;
}
可在main()中这样调用:
testArrayArg2(a, sizeof(a) / sizeof(a[0]));
这样,testArrayArg2()中便可安全地访问数组元素了。
1. 数组作为函数参数,传值还是传址?
2. 函数参数中的数组元素个数能否确定?
先看下面的代码。
[cpp] view
plaincopy
#include <iostream>
using namespace std;
void testArrayArg(int a[])
{
cout << endl;
cout << "in func..." << endl;
cout << "array address: " << a << endl;
cout << "array size: " << sizeof(a) << endl;
cout << "array element count: " << sizeof(a) / sizeof(a[0]) << endl;
cout << "changing the 4th element's value to 10." << endl;
a[3] = 10;
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
cout << "in main..." << endl;
cout << "array address: " << a << endl;
cout << "array size: " << sizeof(a) << endl;
cout << "array element count: " << sizeof(a) / sizeof(a[0]) << endl;
testArrayArg(a);
cout << endl << "the 4th element's value: " << a[3] << endl;
return 0;
}
运行结果如下:
in main...
array address: 0012FF4C
array size: 20
array element count: 5
in func...
array address: 0012FF4C
array size: 4
array element count: 1
changing the 4th element's value to 10.
the 4th element's value: 10
当我们直接将数组a作为参数调用testArrayArg()时,实参与形参的地址均是0012FF4C。并且,在testArrayArg()中将a[3]的值修改为10后,返回main()函数中,a[3]的值也已经改变。这些都说明C++中数组作为函数参数是传址。
特别需要注意的是,在main()中,数组的大小是可以确定的。
array size: 20
array element count: 5
但作为函数参数传递后,其大小信息丢失,只剩下数组中第一个元素的信息。
array size: 4
array element count: 1
这是因为C++实际上是将数组作为指针来传递,而该指针指向数组的第一个元素。至于后面数组在哪里结束,C++的函数传递机制并不负责。
上面的特性可总结为,数组仅在定义其的域范围内可确定大小。
因此,如果在接受数组参数的函数中访问数组的各个元素,需在定义数组的域范围将数组大小作为另一辅助参数传递。则有另一函数定义如下:
[cpp] view
plaincopy
void testArrayArg2(int a[], int arrayLength)
{
cout << endl << "the last element in array is: " << a[arrayLength - 1] << endl;
}
可在main()中这样调用:
testArrayArg2(a, sizeof(a) / sizeof(a[0]));
这样,testArrayArg2()中便可安全地访问数组元素了。
相关文章推荐
- C++数组作为函数参数的几个问题
- C++数组作为函数参数的几个问题
- C++数组作为函数参数的几个问题
- C++数组作为函数参数的几个问题
- C++数组作为函数参数的几个问题(转)
- C/C++面试题--数组作为函数参数的几个问题
- 关于C51的中断函数要注意的几个问题 -----中断中调用函数参数错误问题
- 指针作为函数的参数传递的问题
- 指针作为函数的参数传递的问题
- Django model作为函数参数的问题
- 关于C++当中的函数作为另一个函数的参数之小问题
- c++之指针作为函数参数传递的问题
- 记JSON.stringify的用法(可以传入函数作为参数解决大小写问题)
- 关于二维数组作为参数传递给函数的问题
- 实例解析使用指针作为函数参数传递需要注意的问题
- c++之指针作为函数参数传递的问题
- C++中关于将fstream对象作为函数参数传递相关问题说明
- 指针作为函数的参数传递的问题
- 指针变量作为函数参数问题
- 关于C/C++的二维数组作为函数参数问题