Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef of array of typedefs of array

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?

like image 626
user2340939 Avatar asked May 29 '16 05:05

user2340939


People also ask

Can you typedef an array?

In C programming language, typedef is also used with arrays.

What is typedef declaration?

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.

What is a typedef struct?

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.

Is typedef scoped?

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.


2 Answers

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
like image 149
Shangtong Zhang Avatar answered Oct 08 '22 22:10

Shangtong Zhang


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].

like image 30
songyuanyao Avatar answered Oct 08 '22 22:10

songyuanyao