What is typedef int var [1]; do?

I came across the following code:

typedef int var[1]; // or var[3]

What is he really doing? I don’t understand what the index is adding, since now I can define "var" for int

+5
source share
2 answers

varis a type definition for an array of intsize one.

You can write

var x;

and xwill be a type variableint[1]

+2
source

typedefdeclarations use the same syntax as regular variable declarations. The difference is that instead of declaring "a variable with a xtype name , y" you declare a "type with a name xthat is synonymous with the type y." The syntax is otherwise the same.

, typedef , :

int var[1];

var - , int[1], int. typedef :

typedef int var[1];

var int[1].

:

int (*fp)(int);         // fp is a function pointer variable
typedef int (*fp)(int); // fp is a function pointer type

, identity,

template <typename T> struct identity { typedef T type; };

, . , :

typedef identity<int[1]     >::type var; // array type
typedef identity<int(*)(int)>::type fp;  // function pointer type

, , , , identity:

identity<int[1]     >::type var; // array variable
identity<int(*)(int)>::type fp;  // function pointer variable
+8

All Articles