简体   繁体   English

使用typedef结构

[英]Use of typedef struct

typedef struct pjmedia_endpt pjmedia_endpt;

declaration of struct pjmedia_endpt endpt and pjmedia_endpt endpt are no ok("Variable has incomplete type struct pjmedia_endpt endpt"). 声明结构pjmedia_endpt endpt和pjmedia_endpt endpt不好(“变量的结构struct pjmedia_endpt endpt类型不完整”)。

i want to declare a variable of pjmedia_endpt, how to do. 我想声明pjmedia_endpt的变量,怎么办。

Until unless you complete the incomplete type like this, 除非您完成这样的不完整类型,

struct pjmedia_endpt
{
   member1;
   member2;
   ...
};

you can not use pjmedia_endpt . 您不能使用pjmedia_endpt

Once you complete the structure, then you can use 完成结构后,即可使用

pjmedia_endpt temp; // Declare a variable to it

Your typedef establishes a forward declaration. 您的typedef建立一个前向声明。 After this forward declaration you can define variables of type "pointer to pjmedia_endpt ", like this: 在此向前声明之后,您可以定义类型为“指向pjmedia_endpt指针”的变量,如下所示:

pjmedia_endpt *ptrVar;

However, forward declaration by itself is not enough to declare variables or struct fields until the actual definition is provided. 但是,仅在提供实际定义之前,向前声明本身不足以声明变量或struct字段。 You need to define the actual structure of pjmedia_endpt before you could start using your newly defied type pjmedia_endpt for variable declarations: 您需要先定义pjmedia_endpt的实际结构,然后才能开始使用新定义的类型pjmedia_endpt进行变量声明:

struct pjmedia_endpt {
    ...
};

You must use typedef to define a new type based on a predefined structure or a type. 您必须使用typedef来基于预定义的结构或类型定义新类型。 Otherwise you cannot declare a variable with that new type unless you define the pjmedia_endpt structure. 否则,除非定义pjmedia_endpt结构,否则无法使用该新类型声明变量。

First define your structure: 首先定义您的结构:

struct pjmedia_endpt{
    // ...
};

Then you can assigne a type name to your structure: 然后,您可以为您的结构分配类型名称:

typedef struct pjmedia_endpt pjmedia_endpt;

Then you can declare a variable with the new type: 然后,您可以使用新类型声明一个变量:

pjmedia_endpt someVariable;

here is a function need pjmedia_endpt. 这是一个需要pjmedia_endpt的功能。 pjmedia_transport_udp_create(pjmedia_endpt* endpt,...); pjmedia_transport_udp_create(pjmedia_endpt * endpt,...);

actually i what i need is a pointer,so instead 实际上我需要的是一个指针,所以

//declare a pointer.
pjmedia_endpt* endpt;
//do init endpt.then pass pointer to the function.
pjmedia_transport_udp_create(endpt,...);
typedef struct pjmedia_endpt{
    // ...
};

// Now Rename the structure

pjmedia_endpt newname;

// And now you can access the member of structure with newname.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM