Reputation: 685
#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;
}
I get a runtime error when i input the first number, any help will be appreciated I think the problem is in the way i pass the arrays to the functions but I am not really sure the aim of the program is to get input for 10 int-s in the array m, than transfer those divisible by 3 to v, and output both
Upvotes: 1
Views: 870
Reputation: 3
It does not work to pass arrays into c++. This will result in a run time error unless you use pointers or create a class that contains an array and pass that.
Upvotes: 0
Reputation: 385274
void input(int(&x)[10])
In this function signature:
x
is the argument nameint(&)[10]
is the argument type (reference to an array of 10 int
s). Now consider how you've called this function, presumably by copy/paste:
input((&m)[10]);
With any function call, you write just the name of a variable, not its type; so, when you're passing the array to the function, you should just write:
input(m);
(At present you're passing something else that does not exist in memory, because of &
's triple meaning.)
Same goes for the other places where you pass [references to] arrays.
Upvotes: 0
Reputation: 409364
You are passing pointers to the functions, not references.
input((&m)[10]);
In the line above, the &
is getting the address of m
, and making it into a pointer.
The functions are not expecting pointers. Skip the &
in the calls, and it should work.
Also, I suggest you try to find a tutorial describing the difference between a pointer and a reference. A quick Google search turned up this question here on Stack Overflow, with some good answers.
Upvotes: 0
Reputation: 70030
The syntax of passing an array is not correct (though it compiles). It should be as simple as,
input(m);
read(m, 10);
c = copy(m, v);
read(v, c);
int(&x)[10]
should be used for the function prototype where you are receiving an array of size 10 by reference. While calling the function simply pass the name of the variable.
Upvotes: 3
Reputation: 101
Expanding on the answer above: m[10] is past the last element in the array so what happens is you are reading memory outside your variables which is generally considered to be bad form.
Upvotes: -1
Reputation: 254631
input((&m)[10]);
This doesn't do what you think it does; &m
is a pointer to an array, so (&m)[10]
is a reference to the 10th element of an array of arrays; in other words, to an invalid block of memory some distance off the end of m
.
You want input(m);
to pass a reference to m
.
Upvotes: 4