问题描述
我尝试以下code
#include <stdio.h>
int main(void)
{
typedef static int sint;
sint i = 10;
return 0;
}
和命中以下错误:
error: multiple storage classes in declaration specifiers
当我提到的C99规范,我才知道,的typedef
是存储类
。
When I referred the C99 specification, I came to know that typedef
is a storage class
.
6.7.1 Storage-class specifiers
Syntax
storage-class-specifier:
typedef
extern
static
auto
register
Constraints: At most, one storage-class specifier may be
given in the declaration specifiers in a declaration
Semantics: The typedef specifier is called a ‘‘storage-class specifier’’
for syntactic convenience only;
(基于一些互联网搜索和交叉在C99规格指各部分),我能找到的唯一解释是语法上的方便才使语法简单
。
我在找一些理由/解释上的类型名称怎么能有存储类说明?
I'm looking for some justification/explanation on how can a type name have storage class specifier?
不有意义有一个code像的typedef静态INT SINT;
Doesn't it make sense to have a code like typedef static int sint;
?
或我在哪里去了?!
推荐答案
是的typedef
是一个存储类说明符如您在标准中。在某种程度上这是一个语法上的便利,但它是经过深思熟虑的,你可以有的typedef
的或的比较明显的存储类说明符之一。
Yes, typedef
is a storage-class-specifier as you found in the standard. In part it's a grammatical convenience, but it is deliberate that you can either have typedef
or one of the more "obvious" storage class specifiers.
一个typedef声明的类型创建一个别名。
A typedef declaration creates an alias for a type.
在声明静态INT X;
类型X
是 INT
。 静态
无关的类型。
In a declaration static int x;
the type of x
is int
. static
has nothing to do with the type.
(想想看,如果你把 X
,&安培的地址; X
的类型为int *
为int * Y =&放大器; X;
将是合法的,因为会静态INT * Z =&放大器; X
,但后者静态
影响存储类以Z
,并且是独立于存储的类 X
)
(Consider that if you take the address of x
, &x
has type int*
. int *y = &x;
would be legal as would static int *z = &x
but this latter static
affects the storage class of z
and is independent of the storage class of x
.)
如果像这样被允许的静态
将不得不为没有对象被宣布无效。被别名的类型只是 INT
。
If something like this were allowed the static
would have no effect as no object is being declared. The type being aliased is just int
.
typedef static int sint;
这篇关于是的typedef存储类说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!