Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use typedef to alias C++ arrays? [closed]

Tags:

c++

c

when using typedef for arrays, it is used as

typedef int Color[3];

it is very counter-intuitive for me.

why not typedef int[3] Color[3]?

like image 640
Adam Lee Avatar asked Jan 20 '26 18:01

Adam Lee


2 Answers

typedef looks exactly like a normal vairable declaration, and that happens to be how an array declaration looks in C (and inherited into C++):

int foo[3]; // Array of three int
typedef int FooT[3]; // typedef for array of three int.
like image 167
Mark B Avatar answered Jan 23 '26 07:01

Mark B


Typedef simply works like a varaible definition and follows all the same rules (rules that are already established and known), with the only difference that instead of a variable name it denotes the type.

I would say, rather than implementing some new scheme (that would have to handle all the subtle things like, just for sake of example, pointers to arrays vs. arrays of pointers), using an existing scheme is the sensible choice.

like image 41
Terminality Avatar answered Jan 23 '26 08:01

Terminality



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!