2

I'm learning c ++ And the syntax of putting a std::array into the function confuses me.

#include <iostream>
#include <array>

using namespace std;

void printArray(const std::array<int, 5> &n)
{
    std::cout << "length: " << n.size() << endl;

    for (int j = 0; j < n.size(); j++ )
    {
        cout << "n[" << j << "] = " << n[j] << endl;
    }

}

int main()
{

    array<int, 5> n = {1,2,3,4,5};

    printArray(n);

    return 0;
}
  1. I want to ask about 'const', what role does it play and what effect does it have if not using it?

  2. Why do we have to use &n while the name of an array is already pointer

t.niese
  • 39,256
  • 9
  • 74
  • 101
Quốc Cường
  • 417
  • 4
  • 12
  • 1
    `const` isn't strictly related to arrays, it's a keyword you put on things to mean they can't be modified. And an array is not a pointer, the `&` means it's a reference. – Etienne de Martel Jun 02 '19 at 05:09
  • 1
    "*the name of an array is already pointer*" `std::array` does not work like an `int[5]` array. – Nicol Bolas Jun 02 '19 at 05:10

5 Answers5

4

Depending on the argument you can do certain assumptions about the function.

void printArrayA(std::array<int, 5> n)

If I call printArrayA then the array I pass to it is copied, so the function can't do changes to the array I pass, but has an overhead of copying the array.

void printArrayB(std::array<int, 5> &n)

If I call printArrayB then the array I pass to it is not copied, the function could do changes on the array, or on the elements stored in the array.

void printArrayC(const std::array<int, 5> &n)

If I call printArrayC then the array I pass to it is not copied, and because it is const the function can't do any changes on that array or on its elements. (Well I, in theory, could cast away the const, but that's not something that should be done, a const should indicate the caller, that the object won't be changed)

t.niese
  • 39,256
  • 9
  • 74
  • 101
3

Your question is not only about 'std::vector', it is about 'const' and 'references'.

  1. 'const' keywords means that you can call only 'const' method of that class, means that (assume that the used class is implemented correctly) you can't modify this class in that method.

  2. '&' means that you pass the parameter by 'reference' and not by 'value', if you are not familiar with that difference you may want to read this: Value vs. Reference

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
OdedR
  • 133
  • 7
3
void printArray(const std::array<int, 5> &n)

What this does is to allow you to pass in to the function an unchangeable (without const-casting it, anyway) reference to the std::array. The reference is a lot smaller than the array itself, typically just a pointer, and the const bit ensures the function does not attempt to change the underlying object.

It's usually used to ensure you don't copy "large" things where unnecessary, things like vectors, arrays or strings.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

In this case const means that printArray will not modify the object passed to it.

An std::array is not a C-style array (such ase int a[10]), so you are not passing a pointer, you are passing a reference to an std::array object.

jkb
  • 2,376
  • 1
  • 9
  • 12
3

As others have mentioned, the name of a std::array does not decay to a pointer. To convert to a pointer, you would call .data().

Why do you want to pass by (const) reference, and why does a foo[]decay to a foo* const in many contexts?

You (practically) never want to copy arrays. It takes a huge amount of time and memory. You always want to pass them to a function by reference. On top of that, most algorithms that work on arrays are supposed to work on arrays of any size. (Brian Kernighan, the K in K&R, particularly considered it a flaw in Pascal that the size of an array is part of its type.) Therefore, back in the ’70s, the designers of C made a rule that passing an array to a function is the same as passing a pointer to its first element.

C++ stuck with that for the sake of backward compatibility: a lot of programmers compile C code in C++ compilers. One famous pitfall is trying to take the sizeof an array parameter in C++. Because array parameters decay to pointers, this gives you the size of a pointer. In C++, you have the option of passing a type (&name)[size] instead; that is, passing a type[size] by reference. This preserves the size as part of the type. (C also got some new syntax in 1999 that never made it over to C++, including the ability to pass the size of an array parameter.)

In C++, a std::array has no special syntax and works like any other type. You never want to copy an entire array that you aren’t going to modify. You still want to pass arrays by reference, so you use the standard syntax for that: &. Whenever you don’t need to modify a parameter, you declare it const. This helps the compiler detect logic errors and, sometimes, optimize.

Davislor
  • 14,674
  • 2
  • 34
  • 49