C中的typedef数组类型 [英] typedef array type in C
问题描述
typedef int arr [10]
我了解以上声明为int [10]定义了新名称这样
I understand that the above statement defines a new name for int[10] so that
arr intArray;
等效于
int intArray[10];
但是,我对执行此操作的约定感到困惑.在我看来
However, I am confused by the convention for doing this. It seems to me that
typedef int arr [10]
令人困惑,对我来说一个清晰的方法是
is confusing and a clear way to me is
typedef int [10] arr
即我将"int [10]"定义为一种称为arr
i.e. I define the "int[10]" to be a new type called arr
但是,编译器不接受.
请问为什么?这只是C语言的约定吗?
May I ask why ? Is it just a convention for C language ?
推荐答案
非常早期的C版本没有 typedef
关键字.当它被添加到语言中时(1978年之前的某个时候),它必须以与现有语法一致的方式来完成.
Very early versions of C didn't have the typedef
keyword. When it was added to the language (some time before 1978), it had to done in a way that was consistent with the existing grammar.
在语法上,关键字 typedef
被视为存储类说明符,例如 extern
或 static
,甚至尽管它的含义完全不同.
Syntactically, the keyword typedef
is treated as a storage class specifier, like extern
or static
, even though its meaning is quite different.
所以就像你可能写的那样:
So just as you might write:
static int arr[10];
您会写:
typedef int arr[10];
将 typedef
声明视为类似于对象声明.区别在于它创建的标识符(在这种情况下为 arr
)是类型名称,而不是对象名称.
Think of a typedef
declaration as something similar to an object declaration. The difference is that the identifier it creates (arr
in this case) is a type name rather than an object name.
这篇关于C中的typedef数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!