Is there a way to typedef a 2 dimensional array in C? Something like:
有办法在C中定义一个二维数组吗?喜欢的东西:
typedef char[10][10] board;
This example doesn't compile. Is there any way to do it? Or any other solution?
这个例子不编译。有什么办法吗?或任何其他解决方案?
2 个解决方案
#1
15
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"};
#2
5
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 :
你可以像余浩说的那样使用它,也可以用它和char指针一起来定义一个二维数组:
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.
然后你可以按照余浩的描述去做。这样,您就不需要硬编码您希望用于字符串的字符数。
#1
15
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"};
#2
5
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 :
你可以像余浩说的那样使用它,也可以用它和char指针一起来定义一个二维数组:
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.
然后你可以按照余浩的描述去做。这样,您就不需要硬编码您希望用于字符串的字符数。