【发布时间】:2017-09-18 04:57:12
【问题描述】:
#include<iostream>
using namespace std;
int number(int (&a)[10])
{
int n= sizeof(a)/sizeof(a[0]);
return n;
}
int main()
{
int n;
cout << " Enter the number of elements ";
cin >> n;
int a[10];
cout << "Enter array elements : ";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<" The number of elements according to main is "<< sizeof(a)/sizeof(a[0])<<endl;
cout<<" The number of elements in the function number is " << number(a);
}
在上面的代码中的主函数“number(a)”中,我们到底传递了什么,方法取了什么。以及代码在函数中是如何工作的。如果我们在数字函数中不使用“&”符号会发生什么。
我的理解是我们将指向第一个元素的指针传递给函数,但我不知道正确。
【问题讨论】:
-
代码在几个不同的级别上确实非常非常错误。无论你想学习什么,你都应该参考一个完全不同的例子。
-
它是对 10 个整数数组的引用。 cdecl.org/?q=int+%28%26a%29%5B10%5D您可以使用模板对其进行概括:stackoverflow.com/questions/2384107/…
-
@Saustin 这是一道考试题,我们需要解释答案中的输出。
-
这是一道考试题?哎呀。您可以回答很多问题,但它们都可能是错误的,因为我非常怀疑您的教授知道他在做什么。我强烈建议您在线查看不同的示例,以获得比您的教授更好的理解。
-
当有人输入 50 作为项目数并在分配的内存之外写入时会发生什么?似乎是一个相当愚蠢的考试问题。
标签: c++