C++ arrays as function arguments

2020-01-31 03:23发布

  • 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?

8条回答
够拽才男人
2楼-- · 2020-01-31 04:09
  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.
查看更多
聊天终结者
3楼-- · 2020-01-31 04:12

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.

查看更多
ら.Afraid
4楼-- · 2020-01-31 04:19

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.

查看更多
够拽才男人
5楼-- · 2020-01-31 04:21

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.

查看更多
够拽才男人
6楼-- · 2020-01-31 04:22

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).

查看更多
我只想做你的唯一
7楼-- · 2020-01-31 04:23

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.

查看更多
登录 后发表回答