tags:

views:

243

answers:

8
  • Can I pass arrays to functions just as I would do with primitives such as int and bool?
  • Can I pass them by value?
  • How does the function know of the size of the array it is passed?
+1  A: 

You can pass a std::vector and similar well-designed object by value (though this is rather uncommon). Typically, you pass by reference or const reference.

A C/C++ array, as in

void foo(int x[])

is always passed by reference. Also, the function can not determine the true size of the argument passed in by the caller, you have to assume a size (or pass it as separate parameter).

peterchen
David Rodríguez - dribeas
+1  A: 
  1. Yes. For example: void f(int a[]); and call it like this: int myArr[size]; f(myArr);
  2. No, arrays are automatically passed by reference. You have to wrap your array in a struct or class if you want to simulate passing by value.
  3. It doesn't. You have to pass the size yourself.
IVlad
David Rodríguez - dribeas
+19  A: 

Can I pass arrays to functions just as I would do with primitives such as int and bool?

Yes, but only using pointers (that is: by reference).

Can I pass them by value?

No. You can create classes that support that, but plain arrays don't.

How does the function know of the size of the array it is passed?

It doesn't. That's a reason to use things like vector<T> instead of T *.

Clarification

A function can take a reference or pointer to an array of a specific size:

void func(char (*p)[13])
{
    for (int n = 0; n < 13; ++n)
        printf("%c", (*p)[n]);
}

int main()
{
    char a[13] = "hello, world";
    func(&a);

    char b[5] = "oops";
    // next line won't compile
    // func(&b);

    return 0;
}

I'm pretty sure this is not what the OP was looking for, however.

egrunin
Upvoted because I was typing this exact same answer before it became visible.
jonny
Arrays in C++ can be passed by reference in which case you can know the size of the array. Of course, that doesn't apply for arrays created at run-time.
AraK
@AraK - Not sure what you mean here. Can you add an example to your answer?
egrunin
Too many people think about C/C++ :/
Matthieu M.
@egrunin The example is there actually.
AraK
@egrunin: and it does not need to be templated: `void foo( int (` and then call the previous function: `foo(*p)`.
David Rodríguez - dribeas
@AraK, @David: I know the permutations, just wasn't sure we were thinking of the same one. Have appended a simple example.
egrunin
Steve Jessop
@Steve - fixed. Overhasty cut-and-paste.
egrunin
Now `p` is a pointer-to-array, so use it like `(*p)[n]`.
Steve Jessop
@Steve: Fixed. This demonstrates [1] that edge cases take the most time [2] why this syntax is rarely encountered :)
egrunin
+9  A: 

You can pass arrays the usual way C does it(arrays decay to pointers), or you can pass them by reference but they can't be passed by value. For the second method, they carry their size with them:

template <std::size_t size>
void fun( int (&arr)[size] )
{
   for(std::size_t i = 0; i < size; ++i) /* do something with arr[i] */ ;
}

Most of the time, using std::vector or another sequence in the standard library is just more elegant unless you need native arrays specifically.

AraK
That's hardly them carrying their size with them ;) The size is available as a template argument (or you could have a non-templated version with a suitable `const` defined somewhere). You can't get the size from `arr` itself which is where I think your current wording is a little misleading.
Troubadour
David Rodríguez - dribeas
Troubadour
+1  A: 

Yes, you can pass them the same as primitive types. Yes, you can pass by value if you really wanted to with some wrapper code, but you probably shouldn't. It will take the size of the function prototype as the length - which may or may not match the incoming data - so no it doesn't really know.

Or!

Pass a reference or const reference to a vector and be safer and have the size info at your finger tips.

Michael Dorgan
+1  A: 

You can pass an array, but you must pass the size along with it if you want to know it for sure:

function(array, size);

Arrays are always passed by reference and the function can be written one of two ways:

Declaration:    void function (class*, int);
Implementation: void function(class array[]; int size) {}

or

Declaration:    void function (class*, int);
Implementation: void function(class *array; int size) {}

When you pass an array to a function the function in essence receives a pointer to that array, as seen in the second example above. Both examples will accomplish the same thing.

Mike Webb
+2  A: 

Can I pass arrays to functions just as I would do with primitives such as int and bool?

Yes, you can. Passing an array name as an argument to a function passes the address of the first element.

In short:

void foo(int a[]);

is equivalent to

void foo(int * a);

Can I pass them by value?

Not really. The "value" that gets passed with an array name is the address of the first element.

The only way to pass a copy of a C-style array is to wrap it in a class or struct that implements deep-copy semantics.

How does the function know of the size of the array it is passed?

It doesn't, unless you have your function take in an additional parameter that is the size of the array.

JohnMcG
Re: equivalence between `int a[]` and `int * b`: there is one difference, though: the first form does not allow you to play with the pointer (e.g. `a++` is illegal).
RaphaelSP
@RaphaelSP, nope there is no difference like that. The first form does allow `a++` likewise - they are completely equivalent. @John, "There is no way to accept or pass a copy of a C-style array" is wrong when pedantically, though. If you wrap the array into a struct and take the struct by-value, the wrapped array is copied, and the function receives a copy of the c-style array.
Johannes Schaub - litb
@RaphaelSP: `void bar( int a[] ) { ++a; *a = 7; }` compiles both in g++ and comeau.
David Rodríguez - dribeas
Right -- I tried that as well -- I was going to say int * const a, then tried it and saw that you could modify the pointer.I'll clarify "no way" to something less absolute.
JohnMcG
@Johannes, @dribeas: Oh wow, I didn't know array variables and array parameters were different. So much for my infallible knowledge.
RaphaelSP
+1  A: 

C-style arrays behave very strangely when passed into functions. For fixed-sized arrays, I would recommend std::array<int, 10> instead, which can be passed by value like built-in types. For variable-sized arrays, I recommend std::vector<int>, as suggested in other answers.

FredOverflow