c语言 struct 和 typedef struct定义 运行时又什么区别?

kkkkkkkkkkkkikkkkk 2016-10-18 10:25:22
定义的区别就不用说了。
这两种定义方式在运行时,或者其他方面又什么区别?

我把程序编译成汇编,代码生成的都是一样的。
似乎是没有区别?

请教请教
...全文
409 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2016-10-25
  • 打赏
  • 举报
回复
// C/C++ 结构体声明:
struct struct1 { int dummy; };
typedef struct { int dummy; } STRCUT2;
typedef struct struct3 { int dummy; } STRCUT3;

// C/C++ 结构体定义:
struct1 xxx1;           // 仅 C++
struct struct1 xxx1;    // C/C++

STRCUT2 xxx1;           // C/C++

struct3 xxx1;           // 仅 C++
struct struct3 xxx1;    // C/C++
STRCUT3 xxx1;           // C/C++
除了简化写法之外,没什么区别,而且 C++ 支持省略 struct 关键字。之所以开源项目喜欢直接写 struct xxx 之类的,可能是为了体现这是一个结构体。不像 Windows 编程中,宏、数据类型、枚举、结构体一律大写,会给初学编程的人带来很大困惑。
sdghchj 2016-10-18
  • 打赏
  • 举报
回复
struct student { } typedef struct { } student; 我想说的是struct student a和student a有什么区别??
kkkkkkkkkkkkikkkkk 2016-10-18
  • 打赏
  • 举报
回复
最近看了几个开源项目的代码。 我发现他们似乎更喜欢用struct 而不是typedef struct。 如果没区别的话,为什么不用typedef? 这样写起来不也更简单么
qq_15369501 2016-10-18
  • 打赏
  • 举报
回复
typedef,定义别名。
赵4老师 2016-10-18
  • 打赏
  • 举报
回复
typedef typedef type-declaration synonym; The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();" struct struct [tag] { member-list } [declarators]; [struct] tag declarators; The struct keyword defines a structure type and/or a variable of a structure type. See Anonymous Structures and Unsized Array in a Structure for more information. A structure type is a user-defined composite type. It is composed of "fields" or "members" that can have different types. In C++, a structure is the same as a class except that its members are public by default. Using a Structure In C, you must explicitly use the struct keyword to declare a structure. In C++, this is unnecessary once the type has been defined. You have the option of declaring variables when the structure type is defined by placing one or more comma-separated variable names between the closing brace and the semicolon. For related information, see class, union, and enum. Example 1 struct PERSON // Declare PERSON struct type { int age; // Declare member types long ss; float weight; char name[25]; } family_member; // Define object of type PERSON struct PERSON sister; // C style structure declaration PERSON brother; // C++ style structure declaration sister.age = 13; // assign values to members brother.age = 7; Structure variables can be initialized. The initialization for each variable must be enclosed in braces. Example 2 struct POINT // Declare POINT structure { int x; // Define members x and y int y; } spot = { 20, 40 }; // Variable spot has // values x = 20, y = 40 struct POINT there; // Variable there has POINT type struct CELL // Declare CELL bit field { unsigned character : 8; // 00000000 ???????? unsigned foreground : 3; // 00000??? 00000000 unsigned intensity : 1; // 0000?000 00000000 unsigned background : 3; // 0???0000 00000000 unsigned blink : 1; // ?0000000 00000000 } screen[25][80]; // Array of bit fields
paschen 2016-10-18
  • 打赏
  • 举报
回复
C++下不用typedef也可以直接xxx a;
paschen 2016-10-18
  • 打赏
  • 举报
回复
只是写起来简便,生成的汇编代码都一样,运行时没有区别 如果不用typedef,C语言中定义结构体变量要写成: struct xxx a; 使用了可以直接写成xxx a;

69,370

社区成员

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

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