Why does n not equal to 8 in the following function?
void foo(char cvalue[8]) { int n = sizeof (cvalue); } But n does equal to 8 in this version of the function:
void bar() { char cvalue[8]; int n = sizeof (cvalue); } Because you can't pass entire arrays as function parameters in C. You're actually passing a pointer to it; the brackets are syntactic sugar. There are no guarantees the array you're pointing to has size 8, since you could pass this function any character pointer you want.
// These all do the same thing void foo(char cvalue[8]) void foo(char cvalue[]) void foo(char *cvalue) C and C++ arrays are not first class objects; you cannot pass arrays to functions, they always decay to pointers.
You can, however, pass pointers and references to arrays. This prevents the array bounds from decaying. So this is legal:
template<typename T, size_t N> void foo(const T(&arr)[N]) { int n = sizeof(arr); }