typedef有什么用?

问题描述 投票:50回答:8

在C中使用typedef关键字有什么用?什么时候需要?

c typedef
8个回答
49
投票

typedef用于定义某种类型的东西。例如:

typedef struct {
  int a;
  int b;
} THINGY;

...将THINGY定义为给定的结构。这样,您可以像这样使用它:

THINGY t;

...而不是:

struct _THINGY_STRUCT {
  int a;
  int b;
};

struct _THINGY_STRUCT t;

......有点冗长。 typedef可以使一些事情变得更加清晰,特别是指向函数的指针。


30
投票

来自维基百科:

typedef是C和C ++编程语言中的关键字。 typedef的目的是为现有类型指定替代名称,通常是那些标准声明繁琐,可能令人困惑或可能因实现而异的那些。

和:

K&R声称使用typedef有两个原因。首先,它提供了一种使程序更具可移植性的方法。不需要在程序的源文件中出现的任何地方更改类型,只需要更改单个typedef语句。其次,typedef可以使复杂的声明更容易理解。

反对的论点:

他(Greg K.H.)辩称,这种做法不仅会不必要地混淆代码,还会导致程序员意外地误用大型结构,认为它们是简单类型。


13
投票

Typedef用于为现有类型创建别名。它有点像misnomer:typedef没有定义新类型,因为新类型可以与底层类型互换。当底层类型可能发生变化或不重要时,Typedef通常用于界面定义的清晰度和可移植性。

例如:

// Possibly useful in POSIX:
typedef int filedescriptor_t;

// Define a struct foo and then give it a typedef...
struct foo { int i; };
typedef struct foo foo_t;

// ...or just define everything in one go.
typedef struct bar { int i; } bar_t;

// Typedef is very, very useful with function pointers:
typedef int (*CompareFunction)(char const *, char const *);
CompareFunction c = strcmp;

Typedef也可用于为未命名的类型指定名称。在这种情况下,typedef将是所述类型的唯一名称:

typedef struct { int i; } data_t;
typedef enum { YES, NO, FILE_NOT_FOUND } return_code_t;

命名约定不同。通常建议使用trailing_underscore_and_tCamelCase


4
投票

typedef不会引入新类型,但它只为类型提供新名称。

TYPEDEF可用于:

  1. 组合数组,结构,指针或函数的类型。
  2. 为了便于移植,typedef是您需要的类型。然后,当您将代码移植到不同平台时,通过仅在typedef中进行更改来选择正确的类型。
  3. typedef可以为复杂的类型转换提供简单的名称。
  4. typedef也可用于为未命名类型指定名称。在这种情况下,typedef将是所述类型的唯一名称。

注意:-SHOULDNT使用带有结果的TYPEDEF。即使不需要,也要在结构定义中使用标签。


4
投票

来自维基百科:“K&R声称使用typedef有两个原因。首先......其次,typedef可以使复杂的声明更容易理解。”

下面是使用typedef的第二个原因的示例,简化了复杂类型(复杂类型取自K&R“C编程语言第二版第136页)。

char (*(*x([])())

x是一个函数,返回指向函数返回char的指针array []的指针。

我们可以使用typedef使上述声明可理解。请参阅下面的示例。

typedef char (*pfType)(); // pf is the type of pointer to function returning
                          // char
typedef pfType pArrType[2];  // pArr is the type of array of pointers to
                             // functions returning char

char charf()
{ return('b');
}

pArrType pArr={charf,charf};
pfType *FinalF()     // f is a function returning pointer to array of
                     // pointer to function returning char
{
return(pArr);
}

4
投票

在以下示例中解释typedef的用法。此外,Typedef用于使代码更具可读性。

#include <stdio.h>
#include <math.h>

/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/

// typedef a primitive data type
typedef double distance;

// typedef struct 
typedef struct{
    int x;
    int y;
} point;

//typedef an array 
typedef point points[100]; 

points ps = {0}; // ps is an array of 100 point 

// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)

// prototype a function     
distance findDistance(point, point);

int main(int argc, char const *argv[])
{
    // delcare a function pointer 
    distanceFun_p func_p;

    // initialize the function pointer with a function address
    func_p = findDistance;

    // initialize two point variables 
    point p1 = {0,0} , p2 = {1,1};

    // call the function through the pointer
    distance d = func_p(p1,p2);

    printf("the distance is %f\n", d );

    return 0;
}

distance findDistance(point p1, point p2)
{
distance xdiff =  p1.x - p2.x;
distance ydiff =  p1.y - p2.y;

return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
} In front of everything, place the keyword typedef.
    */

2
投票

它可以别名另一种类型。

typedef unsigned int uint; /* uint is now an alias for "unsigned int" */

2
投票
typedef unsigned char BYTE;

在此类型定义之后,标识符BYTE可以用作unsigned char类型的缩写,例如..

BYTE b1,b2;

© www.soinside.com 2019 - 2024. All rights reserved.