有人问了一道C++的选择题,长下面这样
答案是BC,俩选项都对。
core:我的sumarray函数是要传数组名进去,而数组名是数组首地址,实际上我是传地址进去的。
对于二维数组a[2][3]来说,传a[0]实际上是传a[0][0]的地址,所以B对
详见(别人的)另一篇博文:(2条消息) [C/C++]数组与指针——a,a[0],&a,&a[0]区别小结_YupingLiu的技术博客-CSDN博客
而C呢,相当于把实参a[0][0]做一个取地址的行为,所以其实也是传了地址,所以俩都对。
然后在没看上边那个参考文章的时候进行了一些实验
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cassert>
#include<iomanip>//控制精度的,以及各种格式
#include<math.h>//各种数学公式
#include<locale>
using namespace std;
int suma(int a[],int n) {
int sum = 0;
for (int i = 0; i < n;i++) {
sum += a[i];
}
return sum;
}
//三维数组这么传,竟然也是合法的
int main() {
int a[2][2][2] = { 1,2,3,4,5,6,7,8 };
int b = suma(*a[0],8);
cout << b;
}
错误示例:
很碎,很奇怪的一篇