1

I understand the implicit conversion into a pointer. Someone suggested something like this today in some other question:

#include <iostream>

void printArray(int (&a)[5]) {
    for (int i : a) {
        std::cout << i << " ";
    }
}

int main() {
    int a[] = { 1, 2, 3, 4, 5 };
    printArray(a);
}

Questions

  1. Is this the only and the best way of passing an entire array to a function rather than just the pointer to the first element (though inefficient)?

  2. However, if that function were to be written below the main function, what would the function prototype be?

  3. Also, if I were to only use an enhanced for loop to iterate through the elements of an array passed to a function, is there any better way?

Community
  • 1
  • 1
KW123
  • 51
  • 2
  • If you want pass-by-value, you can stuff the array into an aggregate (struct or class) and pass it by value. –  Feb 22 '15 at 06:29
  • @Mints97: Hmmm...you mean like the array as the private data member and no other public functions or constructors? I see the point you are trying to make, but actually implementing it raises some questions for me. Could you be kind enough to illustrate with the code? – KW123 Feb 22 '15 at 06:34
  • Sure, here you go: http://stackoverflow.com/a/4776183/3079266 should work in both C and C++. –  Feb 22 '15 at 06:42
  • Thanks mate. It shows wrapping it in a struct. I hope it's same for a class as well. :) – KW123 Feb 22 '15 at 06:49
  • Yep, the difference is that you'd have to explicitly make the array a `public` field if you use a class ;) –  Feb 22 '15 at 06:50
  • Oh, thank you. But can I also let the array be declared as a private member? – KW123 Feb 22 '15 at 07:13
  • Use `std:array` instead and the syntax is less toxic. – Chris Drew Feb 22 '15 at 07:56
  • You can declare it as private, but that makes little sense - you'd have to do extra work adding getters and setters, which is most probably pointless in this case. In short, don't try to apply object-oriented design principles where they're not needed =) –  Feb 22 '15 at 08:38

1 Answers1

2

1) This does not pass the entire array to the function. It passes a reference to the array. Since the compiler knows the type of the argument, it is able to do appropriate checks (when calling the function) and access array elements (within the function).

2) The declaration of the function (as opposed to the definition/implementation) would be;

void printArray(int (&a)[5]);

The name of the parameter (a) is optional in this.

3) Since printing an object (including an array) does not typically change the object, it would be appropriate for the argument of printArray() to be const-qualified. This also allows the caller to pass a const array (which is not possible in the code as shown). Furthermore, the type of i used in the loop can also be a const reference (which avoids copying elements of the array by value). It would also be possible to use automatic type inference (i.e. auto). Increased const-safety is often viewed as beneficial (since it increases chances of picking up attempts to modify something that should not be changed).

Rob
  • 1,966
  • 9
  • 13
  • Choosing to ignore a in the prototype, it I do this: `void printArray(int & [5]);` it shows as error. :( – KW123 Feb 22 '15 at 06:30
  • The brackets around the & are not optional. – Rob Feb 22 '15 at 06:31
  • Hmmm....ok, could you please explain the meaning of the function signature? What exactly are we passing to the function? – KW123 Feb 22 '15 at 06:42
  • @KW123: we're passing a reference to an array of 5 elements. –  Feb 22 '15 at 06:51
  • 1
    The basic rule of thumb for interpreting declarations (including of function parameters) is start at the inner-most parentheses, go right, then go left. So int (&a) [5] means a is a reference to an array of 5 int. void printArray(int (&a)[5]) is then a function that accepts a reference to an array of 5 int, and returns void. int &a [5] (if it was valid, which it isn't) would mean a is an array of 5 references to int (which is invalid, as arrays of references are not permitted in C++). – Rob Feb 22 '15 at 06:52
  • There is one more doubt buddy. In the function prototype, is the identifier a, which is a reference to the array local to the function? I ask this because it has the same name as that of the original function. – KW123 Feb 22 '15 at 06:59
  • The names of parameters in a "function prototype" (the proper name is function declaration) have no relationship to the names of the same arguments in the function definition. It is actually possible for the function declaration and definition to have different names of the same arguments, since the compiler (in effect) ignores the names in the declaration. It is only the name of function arguments in the actual function definition that matter (assuming they are actually used in the body of the function). – Rob Feb 22 '15 at 07:25