typedef int a[10];怎么解释?

纸-_-鸢 2013-05-19 10:33:46
typedef int a[10];

a b[10]; 为什么分配400个字节的空间?

int a[10];为什么分配了40个字节的空间?

问题:应该怎么解释typedef的这种行为呢?而如果换成是#define a int[10];以上定义则报错?求助!!!
...全文
1225 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Saintyyu 2015-09-08
  • 打赏
  • 举报
回复
#7楼你确定typedef int[10] a不报错?
十年无梦 2013-11-03
  • 打赏
  • 举报
回复
既然typedef int a[10] 和 typedef int[10] a作用相同,那为什么大家习惯前者,而不是后者?后者看起老更加直观且容易理解啊。
赵4老师 2013-05-20
  • 打赏
  • 举报
回复
Typedef Declarations A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared. Typedef names allow you to encapsulate implementation details that may change. A typedef declaration is interpreted in the same way as a variable or function declaration, but the identifier, instead of assuming the type specified by the declaration, becomes a synonym for the type. Syntax declaration : declaration-specifiers init-declarator-list opt ; declaration-specifiers : storage-class-specifier declaration-specifiers opt type-specifier declaration-specifiers opt type-qualifier declaration-specifiers opt storage-class-specifier : typedef type-specifier : void char short int long float double signed unsigned struct-or-union-specifier enum-specifier typedef-name typedef-name : identifier Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways. When a typedef name is used as a type specifier, it can be combined with certain type specifiers, but not others. Acceptable modifiers include const and volatile. Typedef names share the name space with ordinary identifiers (see Name Spaces in Chapter 2 for more information). Therefore, a program can have a typedef name and a local-scope identifier by the same name. For example: typedef char FlagType; int main() { } int myproc( int ) { int FlagType; } When declaring a local-scope identifier by the same name as a typedef, or when declaring a member of a structure or union in the same scope or in an inner scope, the type specifier must be specified. This example illustrates this constraint: typedef char FlagType; const FlagType x; To reuse the FlagType name for an identifier, a structure member, or a union member, the type must be provided: const int FlagType; /* Type specifier required */ It is not sufficient to say const FlagType; /* Incomplete specification */ because the FlagType is taken to be part of the type, not an identifier that is being redeclared. This declaration is taken to be an illegal declaration like int; /* Illegal declaration */ You can declare any type with typedef, including pointer, function, and array types. You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration. Typedef names can be used to improve code readability. All three of the following declarations of signal specify exactly the same type, the first without making use of any typedef names. typedef void fv( int ), (*pfv)( int ); /* typedef declarations */ void ( *signal( int, void (*) (int)) ) ( int ); fv *signal( int, fv * ); /* Uses typedef type */ pfv signal( int, pfv ); /* Uses typedef type */ Examples The following examples illustrate typedef declarations: typedef int WHOLE; /* Declares WHOLE to be a synonym for int */ Note that WHOLE could now be used in a variable declaration such as WHOLE i; or const WHOLE i;. However, the declaration long WHOLE i; would be illegal. typedef struct club { char name[30]; int size, year; } GROUP; This statement declares GROUP as a structure type with three members. Since a structure tag, club, is also specified, either the typedef name (GROUP) or the structure tag can be used in declarations. You must use the struct keyword with the tag, and you cannot use the struct keyword with the typedef name. typedef GROUP *PG; /* Uses the previous typedef name to declare a pointer */ The type PG is declared as a pointer to the GROUP type, which in turn is defined as a structure type. typedef void DRAWF( int, int ); This example provides the type DRAWF for a function returning no value and taking two int arguments. This means, for example, that the declaration DRAWF box; is equivalent to the declaration void box( int, int );
图灵狗 2013-05-19
  • 打赏
  • 举报
回复
typedef int a[10]这样声明之后,相当于数组类型a是int[10]这样的规格;那么a b[10]其实就相当于int b[10][10],10*10*sizeof(int)等于400个字节;而直接用#define a int[10]的话,相当于int[10] b[10]这样的语句是不符合语法规则的。
AnYidan 2013-05-19
  • 打赏
  • 举报
回复
c 语言最让人诟病之一: 复杂类型的定义
自信男孩 2013-05-19
  • 打赏
  • 举报
回复
typedef类型是给类型起别名,typedef int a[10];定义了一个类型即一个10个int类型的数据。所以a b[10];的长度是就是4 * 10 * 10;
begodliker 2013-05-19
  • 打赏
  • 举报
回复
typedef int a[10];是把a定义为拥有10个整型成员的数组,a b[10]就相当于定义一个整型的二维数组 b[10][10],所以要分配400个字节的空间。 #define a int[10]进行的是字符串替换,a b[10]就是 int[10] b[10],当然会报错啦
hugett 2013-05-19
  • 打赏
  • 举报
回复
typedef int a[10];
说明a就等同于int[10]类型。。所以
a b[10];//等同与int b[10][10];
而int a[10];这里的a代表的不是类型。。而是数组名。。 而#define只是字符替换而已。。
a b[10];//会被替换为int[10] b[10];自然是不能通过编译的。。

70,025

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧