wupiku
wupiku

Reputation: 85

What type of typedef is this?

Here is the sample code

typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];

What does the above code means? INT is the alias for int. Rest of the code what is happening?

Upvotes: 1

Views: 106

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

Consider the following declaration

int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];

It declares four variables with type specifier int:

scalar variable INT
pointer *INTPTR
one-dimensional array ONEDARR[10]
and two-dimensional array TWODARR[10][10]

Then using the typedef

typedef int INT, *INTPTR, ONEDARR[10], TWODARR[10][10];

then the names of variables mean not objects but aliases for types that had the variables if they were declared without the typedef.

So INT means the type int, INTPTR means the type int *, ONEDARR means the type int[10], and TWODARR means the type int[10][10].

So now you have a choice whether to declare an array the following way

int a'10][10];

or to specify its type using an alias for the type of the array

TWODARR a;

Consider one more example.

Let's assume you have a function declaration

int f( int x, int y );

It has the type int( int, int ). Now you want to name this type that instead of this long record int( int, int ) to use a shorter record. Then you can use a typedef like

typedef int FUNC( int x, int y );

and as result the name FUNC now denotes the type int( int, int ).

Upvotes: 0

prog-fh
prog-fh

Reputation: 16900

The one-line typedef in the question is a shortcut for

typedef int INT;
typedef int *INTPTR;
typedef int ONEDARR[10];
typedef int TWODARR[10][10];

Then INT is an alias for type int.
INTPTR is an alias for type int *.
ONEDARR is an alias for type int [10].
TWODARR is an alias for type int [10][10].

(https://en.cppreference.com/w/c/language/typedef)

Upvotes: 8

Related Questions