user2216190
user2216190

Reputation: 834

Typedef for 2 dimensional array in C

Is there a way to typedef a 2 dimensional array in C? Something like:

typedef char[10][10] board;

This example doesn't compile. Is there any way to do it? Or any other solution?

Upvotes: 19

Views: 22233

Answers (2)

0decimal0
0decimal0

Reputation: 3984

Type Definition Statement

The type definition statement is used to allow user defined data types to be defined using other already available data types.

Basic Format:

typedef existing_data_type new_user_defined_data_type;

So , yours should be :

typedef char board[10][10];

You can use it as Yu Hao has said OR you can also use it with char pointers to define a 2D array like this :

typedef char *board[10];

And then you can do as described by YU Hao. In such a way you need not hard code the number of characters you want to use for the strings.

Upvotes: 5

Yu Hao
Yu Hao

Reputation: 122383

Try this:

typedef char board[10][10];

Then you can define new array as this:

board double_array = {"hello", "world"}; 

It's the same with:

char double_array[10][10] = {"hello", "world"};

Upvotes: 23

Related Questions