- 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 f开发者_StackOverflow中文版unction know of the size of the array it is passed?
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.
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.
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.
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.
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).
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.
- Yes. For example:
void f(int a[]);
and call it like this:int myArr[size]; f(myArr);
- 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.
- It doesn't. You have to pass the size yourself.
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.
精彩评论