A user posted an answer which says:
So, take the
Hash
by referencevoid topWords(Hash const& t, std::string const& word, std::string* topA);
Also,
string[]
is not a type in C++- ...
The original function posted by the question asker looks like:
void topWords(Hash t, string word, string topA[]);
I'm aware that in Java, the syntax for declaring an array is:
int[] anArray;
whereas in C++ it is:
int anArray[someNumber];
I suspect this is what the answerer was referring to, but maybe they meant something else. So I consulted n3337:
formatting note: irrelevant stuff omitted, newlines added for readability
§ 8.3.4 [dcl.array]
1 In a declaration
T D
whereD
has the formD1 [ constant-expressionopt ] attribute-specifier-seqopt
and the type of the identifier in the declaration
T D1
is “derived-declarator-type-listT
”, then the type of the identifier ofD
is an array type;...
T
is called the array element type;...
If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero. The constant expression specifies the bound of (number of elements in) the array. If the value of the constant expression is
N
, the array hasN
elements numbered0
toN-1
, and the type of the identifier of D is “derived-declarator-type-list array ofN T
”....
Except as noted below, if the constant expression is omitted, the type of the identifier of
D
is “derived-declarator-type-list array of unknown bound ofT
”, an incomplete object type. The type “derived-declarator-type-list array ofN T
” is a different type from the type “derived-declarator-type-list array of unknown bound ofT
”, see 3.9....
So anArray
should be an array of someNumber int
. And topA
is adjusted to be pointer to std::string
. Where does my understanding fail?
In order to avoid invalidating the current answers, I'll post this as an update. Some of you seem to be misunderstanding the nature of my question. I'm not asking about Java vs C++ nor arrays vs pointers, but rather from a language lawyer's point of view why string[]
would not be considered a type.
If we take a look at §3.9/6:
... The declared type of an array object might be an array of unknown size and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points (“array of unknown bound of
T
” and “array ofN T
”) are different types. ... [ Example:extern int arr[]; // the type of array is incomplete
So arr
clearly has a type.