0

Consider the following C/C++ functions:

void print_array_1(int arr[4]) {
    for (int i = 0; i < 4; ++i) {
        std::cout << arr[i] << std::endl;
    }
}

void print_array_2(int arr[]) {
    for (int i = 0; i < 4; ++i) {
        std::cout << arr[i] << std::endl;
    }
}

When passing in a four element array into each functions, they both do the same thing.

Is there any real difference? arr is just a pointer to a chunk of memory, so it seems like they should be equivalent.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ryan
  • 7,621
  • 5
  • 18
  • 31
  • Alternate dupe: https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p – Baum mit Augen Jun 28 '16 at 23:56

1 Answers1

2

No, there is no difference.

The formal argument

int arr[4]

decays to just

int* arr

Which means that you can treat it as a modifiable pointer variable inside the function, and which also means, that all information about the array size is gone.


One way to specify and require a certain array size for a formal argument, is to use std::array:

void foo( std::array<int, 4> const& arr )

Another way is to pass a raw array by reference:

void bar( int (&arr)[4] )
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • So `arr[i]` becomes `*(arr + i)`, right? – The SE I loved is dead Jun 28 '16 at 23:36
  • Yes, by definition. – Cheers and hth. - Alf Jun 28 '16 at 23:37
  • @Cheersandhth.-Alf Or if you want to be a bit more generic, pass by template non-type reference, `template void bar(T (&arr)[N]);` – vsoftco Jun 28 '16 at 23:41
  • Standard wording for this is *"adjusted to pointed"* instead of *"decays to pointer"* (which works on objects, not types). – Baum mit Augen Jun 28 '16 at 23:55
  • @BaummitAugen: No, the common expression is a type decay. And anyway it's a descriptive term. I think it's plain idiocy to differentiate e.g. function template versus template function, and so on. Don't go that way. It's not practical, its only a silly social in-group marker device (which is why I think it's idiocy: it's counter-productive to apply social devices to engineering). – Cheers and hth. - Alf Jun 29 '16 at 00:12
  • @Cheersandhth.-Alf That's the first time I hear that "common expression" (though I heard "the parameter decays" often enough). I disagree with your opinion that distinctions such as this on are idiocy. While one can certainly overdo it, the distinctions at hand is easy enough to understand and avoids enough ambiguity to be useful. (In unambitious contexts like this one it is up to the author I guess.) – Baum mit Augen Jun 29 '16 at 00:41
  • @BaummitAugen: `std::decay::type` is the standard type decay of `T`, since C++11. It's been common terminology since at least 1990 (the ARM), so I'm surprised it's the first time you hear it. We'll have to disagree about the practical utility of changing this terminology to something very specialized; as I see it it's only socially useful, and then only for people who see an advantage in belonging to a group sharing a secret unsmart vocabulary (these folks include doctors, scientists, politicians, etc., and also programmers, but I **hope** not me (crosss fingers etc. etc.!)). – Cheers and hth. - Alf Jun 29 '16 at 02:13