As the title might look very confusing, let me give you an example:
typedef bool foo[2];
typedef foo bar[4];
bar what_am_i;
So, is what_am_i
a [4][2]
dimensional array as I presume, or a [2][4]
dimensional array?
In C programming language, typedef is also used with arrays.
A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared.
The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g., int) and user-defined (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.
A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.
It's bool[4][2]
You can verify it by static_assert:
static_assert(std::is_same<decltype(what_am_i), bool[4][2]>::value, "");
static_assert(std::is_same<decltype(what_am_i), bool[2][4]>::value, ""); // failed
foo
is an array with 2
elements of type bool
, i.e. bool[2]
.
bar
is an array with 4
elements of type foo
, i.e. foo[4]
, each element is a bool[2]
.
Then what_am_i
is bool[4][2]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With