JASON
JASON

Reputation: 7491

Array as parameter

I was wondering which one of these is the best when I pass an array as parameter?

void function(int arr[]) {...};

or

void function(int* arr) {...};

Could you tell me your reason? and which book you might refer to? Thanks!

Upvotes: 5

Views: 344

Answers (6)

gwiazdorrr
gwiazdorrr

Reputation: 6339

Depending on the layer of abstraction you want to provide, you should chose between Olaf's approach or something STL uses to the great level:

template <class Iter>
void function(Iter first, Iter last)
{
    // for example, to get size:
    std::size_t size = std::distance(first, last);
    // for example, to iterate:
    for ( Iter it = first; it != last; ++it )
    {
        // *it to get the value, like:
        std::cout << *it << std::endl;
    }
}

That way, you can use the function not only for arrays, but for various STL types: vectors, lists, queues, stacks and so.

int tab[4] = {1,2,3,4};
function(tab, tab+4);

std::vector<int> vec;
// fill vector with something
function(vec.begin(), vec.end());

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145429

You ask which to choose of

void function(int arr[]) {...};

and

void function(int* arr) {...};

The latter allows you to declare the pointer itself as const, while the former only allows you to declare the items as const. However, to the reader [] indicates an array as opposed to possibly a pointer to a single object. Personally I think the constraint offered by const is more practically useful than the indication of array'ness offered by [].

For higher level code, use std::vector or std::array, whichever is more appropriate.

Upvotes: 1

Olaf Dietsche
Olaf Dietsche

Reputation: 74098

Since this question is tagged , I would use neither. If you must use this, both are equivalent.

But since you use C++, a better approach is to use a std::vector for such tasks

void function(std::vector<int> &arr) {...}

or, if you don't modify the array/vector

void function(const std::vector<int> &arr) {...}

Upvotes: 4

Karthik T
Karthik T

Reputation: 31962

They are semantically identical.

#1 is slightly better (in my opinion) because it makes it explicit that you are passing an array.

Upvotes: 1

If you just want to pass any array (including dynamically allocated), they are equivalent.

Should your function require an actual fixed-size array, you could do this:

template <size_t N>
void function(char (&str)[N]) { ... }

Upvotes: 3

Dariusz
Dariusz

Reputation: 22271

The best way to pass an array as a parameter is actually:

void function(type_t* array, int amountOfElts, int arrayAllocatedElts);

For some cases (like when passing strings) you can often skip these two parameters. But they may be useful either way, for example when doing some operations on the string, or they may save you a strlen call or two.

Using the [] option in function arguments is in my opinion confusing and should be avoided. But I don't think it's a convention.

Upvotes: 0

Related Questions