First, what do you expect? You've defined passedArray
to be
a pointer, so sizeof(passedArray)
will return the size of
a pointer on your machine. What else could it do? I could
understand some confusion if your function was:
void myFunc( char passedArray[15] )
A reasonable person could expect sizeof(passedArray)
to be 15
in this case (but he'd be wrong, because when used as
a parameter, and array is converted by the compiler into
a pointer, and this definition is exactly the same as yours).
The answer to all this is to use std::string
:
void
myFunc( std::string const& passedValue )
{
std::cout << passedValue.size() << std::endl;
}
Unlike char[]
, std::string
actually works. C style arrays,
whether used as strings or not, are broken, and should only be
used in a very limited set of cases, mostly related to objects
with static lifetime and order of initialization issues.