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

CYS8 2003-09-11 03:13:26
请高手赐教,最好有范例~!Thank you ~!
...全文
107 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参数是空的函数指针类型。
学习c 的绝对好书! 第1章 声明和初始化. 1 基本类型 1 1.1 我该如何决定使用哪种整数类型? 1 1.2 为什么不精确定义标准类型的大小? 2 1.3 因为C语言没有精确定义类型的大小,所以我一般都用typedef定义int16和int32。然后根据实际的机器环境把它们定义为int、short、long等类型。这样看来,所有的问题都解决了,是吗? 2 1.4 新的64位机上的64位类型是什么样的? 3 指针声明 3 1.5 这样的声明有什么问题? char *p1, p2; 我在使用p2的时候报错了。 3 1.6 我想声明一个指针,并为它分配一些空间,但却不行。这样的代码有什么问题?char *p; *p = malloc (10); 4 声明风格 4 1.7 怎样声明和定义全局变量和函数最好? 4 1.8 如何在C中实现不透明(抽象)数据类型? 5 1.9 如何生成“半全局变量”,就是那种只能被部分源文件中的部分函数访问的变量? 5 存储类型 6 1.10 同一个静态(static)函数或变量的所有声明都必须包含static 存储类型吗? 6 1.11 extern在函数声明中是什么意思? 6 1.12 关键字auto到底有什么用途? 7 类型定义(typedef) 7 1.13 对于用户定义类型,typedef和#define有什么区别? 7 1.14 我似乎不能成功定义一个链表。我试过typedef struct {char *item; NODEPTR next;}* NODEPTR;但是编译器报了错误信息。难道在C语言中结构不能包含指向自己的指针吗? 7 1.15 如何定义一对相互引用的结构? 9 1.16 Struct {...} x1;和typedef struct{...} x2;这两个声明有什么区别? 10 1.17 “typedef int (*funcptr)();”是什么意思? 10 const限定词 10 1.18 我有这样一组声明:typedef char *charp; const charp p;为什么是p而不是它指向的字符为const? 10 1.19 为什么不能像下面这样在初始式和数组维度值中使用const值?const int n = 5; int a[n];……

69,369

社区成员

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

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