views:

556

answers:

6

Hi, if I am given

void (int a[]) {
a[5] = 3; // this is wrong?
}

Can I do this so that the array that is passed in is modified?

Sorry for deleting, a bit new here...

I have another question which might answer my question:

If I have

void Test(int a) {
}

void Best(int &a) {
}

are these two statements equivalent?

Test(a);
Best(&a);
+2  A: 

If you get the variable not by reference and not by pointer, it means that the function is essentially isolated, getting an ad-hoc copy of a. No matter what you do (without trying to hack the stack or things like that) you wouldn't have access to that value in the calling context.

If you know something about the calling context, you may be able to do things based on some anticipation of stack contents, but it's generally a bad idea.

If your method takes a[] which is essentially a*, then yes, you can alter the contents of the cell that a points to, but you won't be able to alter a (the pointer) itself to point at something else.

Uri
A: 

Nope.

Your options for altering a value from outside the function are call by reference f(int& a), call by pointer f(int* a), and using a global (shudder...) variable.

Drew Hall
I hadn't even thought of using a global! :)
Bill the Lizard
@Bill: That's probably for the best!
Drew Hall
+13  A: 
void Test(int a[]) 
{
    a[5] = 3;
}

just alternate syntax for:

void Test(int* a) 
{
    *(a+5) = 3;
}

No array is passed, just a pointer. The original array is modified.

As for your second revision, given:

void Test(int a) 
{
}

void Best(int &a) 
{
}

then

Test(aa);      // Passes aa by value.  Changes to a in Test() do not effect aa
Best(aa);      // Passes aa by reference; Changes to a DO effect aa
Best(&aa);     // Is a syntax error: Passing a pointer instead of an int.
James Curran
The original works. The only error is that the func has no name!!!!
Martin York
You should know better at 8827 James. Prefer array indexing (which you can do on pointers) rather than pointer arithmatic!!!!!!
Martin York
Thanks. I've fixed the missing function name (I copied it from the question which also had the error). And I never said which syntax is better; I just said that they are equivalent. (I prefer the array syntax too)
James Curran
You are forgivin then ;-)
Martin York
A: 

The primary motivator for passing arrays by reference is to prevent stack overflows and needless copying of large objects. For example, imagine if I had a function like this:

void foo(int x[500000000000]);

The stack would probably overflow the first time you called the function if all arrays were passed by value (but of course this is an obvious exaggeration).

This will become useful when using object-oriented methods. Suppose instead of an array, you had this:

void foo(SomeClass x);

where SomeClass is a class with 500000000000 data members. If you called a method like this, the compiler would copy x bit by bit, which would be a very long process to say the least. The same concept as you use in arrays still applies, but you have to specify that this is to be used by reference manually:

void foo(SomeClass &x);

(and don't go trying to create a 500000000000 element array to begin with unless you have a 64 bit machine and lots of RAM)

Jason Baker
void foo(int x[500000000000]); is the same as void foo(int x[1]); in C / C++ :)
Johannes Schaub - litb
void foo(int x[500000000000]); is equivalent to void foo(int *x);
Dave Hillier
Perhaps I wasn't clear enough. I understand that all arrays are passed by reference. I was attempting to explain WHY arrays are passed by reference.
Jason Baker
A: 

Read the answer given here about the difference of int[] and int* in a parameter list: Difference between char* and char[] . I've really put so much love into that answer! :)

Regarding your question about your Test and Best functions, James Curran provided an excellent answer.

Johannes Schaub - litb
A: 

Your original Function should work.
If you give it a name:

#include <iostream>

// Arrays always de-generate to pointers.
void plop(int a[])  // Make sure this function has a name.
{
    a[5]    = 3;
}

int main()
{
    int test[]  = { 1,1,1,1,1,1,1,1};

    plop(test);

    std::cout << test[5] << std::endl;
}

This is because arrays always de-generate into pointers when passed as an argument to a function. So this should always work as expected. Assuming you don't index beyond the end of the array. Inside plop there is no way to determine the size of the array passed.

Martin York