Reputation: 263118
Everybody knows how to declare an array with constant elements:
const int a[10];
Apparently, it is also possible to declare an array that is itself constant, via a typedef
:
typedef int X[10];
const X b;
From a technical and a practical standpoint, do a
and b
have the same type or different types?
Upvotes: 4
Views: 248
Reputation: 3396
Apparently, it is also possible to declare an array that is itself constant
Nope. In N1256, §6.7.3/8:
If the specification of an array type includes any type qualifiers, the element type is so-qualified, not the array type.118)
Then footnote 118 says:
Both of these can occur through the use of
typedefs
.
Upvotes: 4
Reputation: 153447
Ran some tests:
typedef int X[10];
#define typeinfo(t) _Generic((t), \
X: "int X[10]", \
int: "int", \
int *: "int *", \
const int *: "const int *", \
default: "not" \
)
int main(void) {
const int a[10];
const X b;
printf("%zu\n", sizeof (const int *));
puts(typeinfo(a));
puts(typeinfo(b));
printf("%zu\n", sizeof a);
printf("%zu\n", sizeof b);
puts(typeinfo(a[0]));
puts(typeinfo(b[0]));
printf("%zu\n", sizeof a[0]);
printf("%zu\n", sizeof b[0]);
}
Output
4
const int *
const int *
40
40
int
int
4
4
Both have the same size and both convert to the same type when passed as an argument. Neither is a pointer. Both are arrays.
By code analysis and testing - they are the same type.
Upvotes: 1
Reputation: 19039
They have the same type, though clang will print them differently. Since the array itself cannot be const
, it is said to "fall through".
For the non-typedefed version, clang prints the type as const int [10]
. For the typedef version, it prints int const[10]
. Both of those are equivalent.
Upvotes: 4