C ++ Options Reference

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

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

Sorry for deleting, a little new here ...

I have another question that may answer my question:

If I have

void Test(int a) {
}

void Best(int &a) {
}

      

- are these two statements equivalent?

Test(a);
Best(&a);

      

+1


source to share


6 answers


void Test(int a[]) 
{
    a[5] = 3;
}

      

just an alternative syntax for:

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

      

No array is passed, just a pointer. The original array has been 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.

      

+13


source


If you get a variable not by reference and not by pointer, that means the function is essentially isolated by getting a special copy of a. No matter what you do (without trying to hack the stack or something like that), you will not have access to that value in the calling context.

If you know something about the calling context, you might be doing something based on waiting for some stack content, but this is generally a bad idea.



If your method takes the value [], which is essentially *, then yes, you can change the contents of the cell that point points to, but you cannot change (the pointer itself) to point to something else.

+2


source


Nope.

Your parameters for changing the value outside of the function are a call on reference f (int & a), a call on a pointer f (int * a), and using a global variable (shudder ...).

0


source


Read the answer given here about the difference int[]

and int*

in the parameter list: Difference between char*

andchar[]

. I really fell in love with this answer so much! :)

Regarding your question about your functions Test

and Best

, James Curran provided an excellent answer.

0


source


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 degenerate into pointers when passed as an argument to a function. Therefore, this should always work as expected. Assuming you are not indexing outside of the array. There is no way inside plop to determine the size of the passed array.

0


source


The primary motivator for passing arrays by reference is avoiding and unnecessarily copying large objects. For example, imagine if I had a function like this:

void foo(int x[500000000000]);

      

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

This will be useful when using object oriented methods. Suppose instead of an array:

void foo(SomeClass x);

      

where SomeClass is a class with 500,000,000,000 data members. If you call a method like this, the compiler will copy x piece by piece, which would be a very long process to say the least. The same concept as in arrays still applies, but you have to specify that this should be manually referenced by reference:

void foo(SomeClass &x);

      

(and don't try to create an array of 500,000,000,000 items to start with unless you have a 64-bit machine and lots of RAM)

-1


source







All Articles