views:

265

answers:

3

Are arrays passed by default by ref or value? Thanks.

+8  A: 

They are passed as pointers. This means that all information about the array size is lost. You would be much better advised to use std::vectors, which can be passed by value or by reference, as you choose, and which therefore retain all their information.

Here's an example of passing an array to a function. Note we have to specify the number of elements specifically, as sizeof(p) would give the size of the pointer.

int add( int * p, int n ) {
   int total = 0;
   for ( int i = 0; i < n; i++ ) {
       total += p[i];
   }
   return total;
}


int main() {
    int a[] = { 1, 7, 42 };
    int n = add( a, 3 );
}
anon
Thanks Neil, but as I mentioned in one of my yesterday's post I'm currently working on excersizes from TC++PL and I have to use oridinary arrays. I was just wonder if it is necessary to write something like: void f(int ( or is it just enough to declare as a array without ref.
There is nothing we can do
So basicaly judging by this answer they passed by value which is size of a pointer. Am I right?
There is nothing we can do
@atch a good reason for not learning C++ from TC++PL, IMHO.
anon
@Neil Well I like very much style in which B.S. teaches and writes and I also think that one have to know very low level tools as well as higher abstraction tools. I assume that anyway.
There is nothing we can do
@atch Well if he dosn't explain how arrays are passed to functions, and then expects you to write code that does this, it's not a good teaching book. Which exercise is this, anyway?
anon
@ALL GUYS - Have to go to the shops with me wife. Be back soon;)
There is nothing we can do
@Neil page 164 e. 8;
There is nothing we can do
@atch In that case, see section 7.2.1. But I think that's a bloody awful exercise.
anon
@Neil Yes I do agree with you that this excersize isn't the nicest one but due to the fact that there isn't that much more material available in form of textbooks I'm using this one. And yes some of excersizes in this book are dreadful which I've mentioned to B.S. myself few months ago.
There is nothing we can do
+2  A: 

Arrays are special: they are always passed as a pointer to the first element of the array.

Andreas Bonini
+2  A: 

First, you cannot pass an array by value in the sense that a copy of the array is made. If you need that functionality, use std::vector or boost::array.

Normally, a pointer to the first element is passed by value. The size of the array is lost in this process and must be passed separately. The following signatures are all equivalent:

void by_pointer(int *p, int size);
void by_pointer(int p[], int size);
void by_pointer(int p[7], int size);   // the 7 is ignored in this context!

If you want to pass by reference, the size is part of the type:

void by_reference(int (&a)[7]);   // only arrays of size 7 can be passed here!

Often you combine pass by reference with templates, so you can use the function with different statically known sizes:

template<size_t size>
void by_reference(int (&a)[size]);

Hope this helps.

FredOverflow