Is C++ Array passed by reference or by pointer?

ぐ巨炮叔叔 提交于 2021-01-18 06:00:13

问题


In school, our lecturer taught us that the entire array was passed by reference when we pass it to a function,.

However, recently I read a book. It says that arrays are passed by pointer by default when passing the entire array to a function. The book further mention that "passing by pointer is very similar to passing by reference", which means that passing by pointer and passing by reference are actually different.

It appears that different source stated differently.

So my question is: In C++, are arrays passed by reference or by pointer when we pass the entire array to a function?

For Example:

void funcA(int []); //Function Declaration

int main()
{
    int array[5];
    funcA(array); //Is array passed by ref or by pointer here?
}

回答1:


At worst, your lecturer is wrong. At best, he was simplifying terminology, and confusing you in the process. This is reasonably commonplace in software education, unfortunately. The truth is, many books get this wrong as well; the array is not "passed" at all, either "by pointer" or "by reference".

In fact, because arrays cannot be passed by value due to an old C restriction, there is some special magic that happens with arrays as function arguments.

The function declaration:

void funcA(int[]);

is silently translated into the following:

void funcA(int*);

and when you write this:

funcA(myArray);

it is silently translated into the following:

funcA(&myArray[0]);

The result is that you're not passing the array at all; you pass a pointer to its first element.

Now, at certain levels of abstraction/simplification, you can call this "passing an array by pointer", "passing an array by reference" or even "passing a handle to an array", but if you want to talk in C++ terms, none of those phrases are accurate.




回答2:


The terminology used by your lecturer is confusing. However, in a function declaration such as

void funcA(int []);

the int[] is just another way of saying int*. So funcA can take any argument that is or can be converted to an int*.

Arrays can decay to pointers to the first element in the right context. This means, for example, that you can assign an array's name to a pointer like this:

int array[42];  // array is of type int[42]
int * arr = array; // array decays to int*

So, when you pass array to funcA,

funcA(array); // array decays to int*

funcA has a pointer to the first element of the array.

But it is also possible to pass arrays by reference. It just requires a different syntax. For example

void funcB(int (&arr)[42]);

So, in your example, you are passing a pointer to the first element of the array, due to the signature of your function funcA. If you called funcB(array), you would be passing a reference.




回答3:


Pass-by-pointer is a bit of a misnomer. It doesn't happen in C++. There is only pass-by-value and pass-by-reference. Pointers in particular are passed by value.

The answer to your question is: it depends.

Consider the following signatures:

void foo(int *arr);
void bar(int *&arr);
void baz(int * const &arr);
void quux(int (&arr)[42]);

Assuming you are passing an array to each of these functions:

  • In foo(arr), your array is decayed to a pointer, which is then passed by value.
  • In bar(arr), this is a compiler error, because your array would decay to a (temporary) pointer, and this would be passed by reference. This is nearly always a bug, since the reason you would want a mutable reference is to change the value of the referent, and that would not be what would happen (you would change the value of the temporary instead). I add this since this actually does work on some compilers (MSVC++) with a particular extension enabled. If you instead decay the pointer manually, then you can pass that instead (e.g. int *p = arr; bar(p);)
  • In baz(arr), your array decays to a temporary pointer, which is passed by (const) reference.
  • In quux(arr), your array is passed by reference.

What your book means by them being similar is that passing a pointer by value and passing a reference are usually implemented identically. The difference is purely at the C++ level: with a reference, you do not have the value of the pointer (and hence cannot change it), and it is guaranteed to refer to an actual object (unless you broke your program earlier).



来源:https://stackoverflow.com/questions/24057085/is-c-array-passed-by-reference-or-by-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!