1
#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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
KGS
  • 635
  • 4
  • 19

6 Answers6

4
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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • However, it's interesting that OP's code compiles, if kept as it is. I never knew that. – iammilind Oct 19 '11 at 10:32
  • 3
    @iammilind: `(&m)[10]` is the tenth element of an array of arrays, and so has the correct type for the function. – Mike Seymour Oct 19 '11 at 10:34
  • @MikeSeymour, yes I saw your answer. But then what is the difference between `m[10]` and `(&m)[10]`, if both are 10th element. Does the later syntax creates a reference ? Can we create reference like that ? – iammilind Oct 19 '11 at 10:36
  • @iammilind: It is _not_ the case that both are the 10th element of `m`. Read Mike's answer again! `&m` is an `int(*)[10]` which, as far as this type knows, points to one or more arrays of 10 `int`s each. The OP has only one but with `(&m)[10]` he's trying to pass a reference to the mythical 10th such array of 10 `int`s. That's why it compiles; `m[10]` would not as the function does not accept an `int`. – Lightness Races in Orbit Oct 19 '11 at 10:38
  • @iammilind: `m[10]` is the tenth of an array of integers, an lvalue of type `int`. `(&m)[10]` is the tenth of an array of ten-element arrays, an lvalue of type `int[10]` (which can therefore be bound to a reference to `int[10]`). Its address is the same as `m[100]`. – Mike Seymour Oct 19 '11 at 10:41
0

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.

Cabes234
  • 3
  • 2
0

void input(int(&x)[10])

In this function signature:

  • x is the argument name
  • int(&)[10] is the argument type (reference to an array of 10 ints).

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

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.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
-1

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.

M. Wangel
  • 101
  • 1
  • 1