请问typedef是什么来的??怎么用??

CYS8 2003-09-11 03:13:26
请高手赐教,最好有范例~!Thank you ~!
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sevecol 2003-09-11
  • 打赏
  • 举报
回复
函数指针

善用typedef能起到将程序的表达更加清晰
很多template都用这个来是代码更加清晰。
CYS8 2003-09-11
  • 打赏
  • 举报
回复
谢谢两位的帮助。

但是起这个新类型名 有什么意义呢?
正如上面的typedef int INTEGER;
给整形起了另一个名不是多此一举吗?

这个typedef多用在什么地方或场合?
gai 2003-09-11
  • 打赏
  • 举报
回复
C语言允许用typedef说明一种新类型名,说明新类型名的语句一般形式为:

typedef 类型名 标识符;

在此,“类型名”必须是在此语句之前己有定义的类型标识符。“标识符”是一个用户定义标

识符,用作新的类型名。typedef语句的作用仅仅是用“标识符”来代表己存在的“类型名”,

并末产生新的数据类型。原有类型名依然有效。例如:

typedef int INTEGER;

该语句把一个用户命名的标识符INTEGER说明成了一个int类型的类型名。在此说明之后,

可以用标识符INTEGER来定义整型变量。例如:

INTEGER m,n; 等价于 int m,n;

也就是说:INTEGER是int的一个别名。为了便于识别,一般习惯将新的类型名用大写字母表

示。又如:

typedef char *CHARP;

CHARP p;

等价于: char *p;
sevecol 2003-09-11
  • 打赏
  • 举报
回复
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the type-declaration portion of the declaration.

typedef type-declaration synonym;
You can use typedef declarations to construct shorter or more meaningful names for types already defined by the language or for types that you have declared. Typedef names allow you to encapsulate implementation details that may change.

In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.

You cannot use the typdef specifier inside a function definition.

Typedef names share the name space with ordinary identifiers. Therefore, a program can have a typedef name and a local-scope identifier by the same name.

Example
// typedef_specifier1.cpp
typedef char FlagType;

int main() {
}

void 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. For example:

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.

Examples

One use of typedef declarations is to make declarations more uniform and compact. For example:

typedef char CHAR; // Character type.
typedef CHAR * PSTR; // Pointer to a string (char *).
PSTR strchr( PSTR source, CHAR target );
typedef unsigned long ulong;
ulong ul; // Equivalent to "unsigned long ul;"
To use typedef to specify fundamental and derived types in the same declaration, you can separate declarators with commas. For example:

typedef char CHAR, *PSTR;
The following example provides the type DRAWF for a function returning no value and taking two int arguments:

typedef void DRAWF( int, int );
After the above typedef statement, the declaration

DRAWF box;
would be equivalent to the declaration

void box( int, int );
Example
typedef is often combined with struct to declare and name user-defined types:

// typedef_specifier2.cpp
#include <stdio.h>

typedef struct mystructtag {
int i;
double f;
} mystruct;

int main() {
mystruct ms;
ms.i = 10;
ms.f = 0.99;
printf("%d %f\n", ms.i, ms.f);
}
Output
10 0.990000
CYS8 2003-09-11
  • 打赏
  • 举报
回复
不太明白耶。能不能写段小程序说明一下个中玄机?
sevecol 2003-09-11
  • 打赏
  • 举报
回复
定义类型的别名

typedef int (*p)();//这里p是返回值是int参数是空的函数指针类型。

69,364

社区成员

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

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