How are arrays passed?

30,286

Solution 1

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 );
}

Solution 2

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.

Solution 3

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

Share:
30,286

Related videos on Youtube

There is nothing we can do
Author by

There is nothing we can do

In total love with C++.

Updated on July 09, 2022

Comments

  • There is nothing we can do
    There is nothing we can do almost 2 years

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

  • There is nothing we can do
    There is nothing we can do about 14 years
    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 (&a)[size]); or is it just enough to declare as a array without ref.
  • There is nothing we can do
    There is nothing we can do about 14 years
    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
    There is nothing we can do about 14 years
    @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.
  • Admin
    Admin about 14 years
    @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?
  • There is nothing we can do
    There is nothing we can do about 14 years
    @ALL GUYS - Have to go to the shops with me wife. Be back soon;)
  • Admin
    Admin about 14 years
    @atch In that case, see section 7.2.1. But I think that's a bloody awful exercise.
  • There is nothing we can do
    There is nothing we can do about 14 years
    @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.