Edmund's blog

Writing code.

Passing an array by reference

How do you pass an array by reference in C++?

Like this:

1
2
3
4
5
6
7
void CallMe(const char (&array)[6]);

CallMe("Hello");

// These will not compile
CallMe("Hell");
CallMe("Hellos");

This way you can enforce an exact array size. Any other length will simply fail to compile.