#include <iostream>
#include <cmath>
using namespace std;
void input(int(&x)[10]);
int copy(int (&x)[10], int(&y)[10]);
void read(int(&x)[10], int b);
int main()
{
int m[10], v[10], c;
input((&m)[10]);
read((&m)[10], 10);
c = copy((&m)[10], (&v)[10]);
read((&v)[c],c);
system("PAUSE");
return 0;
}
void input(int(&x)[10])
{
for(int i=0; i < 10; i++)
{
cout<<"enter number "<<i+1<<": ";
cin>>x[i];
}
}
void read(int(&x)[10], int b)
{
for(int i=0; i < b; i++)
{
cout<<"number("<<i+1<<"): "<<x[i];
}
}
int copy(int(&x)[10], int(&y)[10])
{
int c = 0;
for(int i = 0; i < 10; i++)
{
if(x[i] % 3 == 0)
{
y[c]=x[i];
c++;
}
}
return c;
}
当我输入第一个数字时出现运行时错误,我们将不胜感激 我认为问题在于我将数组传递给函数的方式,但我不太确定 该程序的目的是在数组 m 中获取 10 个 int-s 的输入,然后将可被 3 整除的那些转移到 v,并输出两者
最佳答案
input((&m)[10]);
这并不像您认为的那样; &m
是指向数组的指针,所以(&m)[10]
是对数组数组第10个元素的引用;换句话说,到距离 m
末尾一定距离的无效内存块。
您希望 input(m);
传递对 m
的引用。
关于c++ - 将数组 (&) 传递给函数 c++,运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819813/