2
void input(int B[XAM][XAM])
{                int i,j,a;
    for (i=0;i<3;i++) {
        for (j=0; j<3; j++) {
            scanf("%d",&B[i][j]);
        }
    }

    for (i=0;i<3;i++) {
        for (j=0; j<3; j++) {
            printf("%d ",B[i][j]);
        }
        printf("\n");
    }
}


void main()
{
#define XAM 3
    int pilih,A[10],B[3][3],i,j;

    input(&B[XAM][XAM]);

    for (i=0;i<3;i++) {
        for (j=0; j<3; j++) {
            printf("%d ",B[i][j]);
        }
        printf("\n");
    }

    getch(); 
}

why when i try to run the program, I already used "Pass by reference", but still when I call the output from void main, the output didn't the same as my input from the procedure, but if I call the output from the procedure, there is no problem can someone help me why pass the reference didn't work? thanks

Michael
  • 52,337
  • 9
  • 68
  • 113
user138607
  • 21
  • 1
  • 2
    `B[3][3]` (which is the expansion of `B[XAM][XAM]`) in the function call accesses an element which is out of bounds. Also, `&` in the caller code isn't going to pass the argument by reference. The `&` has to be in the signature and the caller code looks like pass by value. Also, multi-dimension arrays have their pitfalls, and I'm not sure if this code is going to work as expected. I very much recommend `std::vector` (for multi dimensions, nest them or index it like `x + y*XAM`) – leemes May 05 '14 at 13:04
  • 1
    Why not use `std::vector` or some other suitable container instead of raw arrays if you're really writing C++ code (i.e. not C)? – Michael May 05 '14 at 13:04
  • main should return an int:http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – user1942027 May 05 '14 at 13:05
  • How can this compile? The compiler does not know what `XAM` is at the declaration point of input. It should abort execution at that point... – RedX May 05 '14 at 13:09

2 Answers2

2

you are passing the address (&) of [3][3] element of B using:

&B[3][3]

Just write

input(B);

Note that passing by reference is not done when you are calling funtcion, but when you are defining, so:

int foo(Foo& f);

passes f as reference when using it simply like foo(f);

But int foo(Foo f);

in case where Foo is not a pointer (your B is a pointer) would end in compilation error, when trying foo(&f).

Also it would be good if you used XAM constant in B definition

B[3][3] -> B[XAM][XAM]

And turn on warnings in your compilator!

zoska
  • 1,515
  • 10
  • 20
1

You want to pass the array by reference.

Whether or not a function parameter is passed by reference or by value is decided at the function's signature. You have to change the parameter type such that it's a reference to an array. That's done by adding the ampersand & to the parameter name.

For arrays, parentheses need to be added which needs getting used to:

void input(int (&B)[XAM][XAM]) {
    // ...
}

When calling that function, simply pass B without mentioning the dimensions or using the & operator (that does something different when used at values!). That means, calling the function is written as if the argument was passed by value (no syntactical difference there), but of course it is passed by reference since the function's signature says so.

input(B);

Indeed, passing arrays by reference is the only way to pass an array as an array. Otherwise (if your parameter looks like int B[XAM][XAM]), only a pointer to the first element is actually passed, and it isn't treated as an array within the function. You can easily check how it is treated in the function body by using the sizeof operator.

Related question: Passing an array by reference

Community
  • 1
  • 1
leemes
  • 42,229
  • 18
  • 115
  • 172