简体   繁体   English

C typedef和指向结构的指针

[英]C typedef and pointers to struct

If I have the following: 如果我有以下内容:

typedef struct _MY_STRUCT
{
   int a;
   float b;
} MY_STRUCT, *PMYSTRUCT

What does *PMYSTRUCT do? *PMYSTRUCT做什么的? Is it now a pointer type which I need to declare or just a pointer to _MY_STRUCT which I can use? 现在是我需要声明的指针类型还是仅可以使用的_MY_STRUCT指针?

I know that MY_STRUCT is a new type that needs to be used as follows: 我知道MY_STRUCT是一种新类型,需要按如下方式使用:

MY_STRUCT str;
str.a = 2;

But what about that *PMYSTRUCT ? 但是那个*PMYSTRUCT呢?

PMYSTRUCT ms = NULL;

等于

MYSTRUCT* ms = NULL;

It will give the same effect as 效果与

typedef MYSTRUCT * PMYSTRUCT; typedef MYSTRUCT * PMYSTRUCT;

It just acts as a typedef to the pointer of the struct. 它只是充当结构体指针的typedef。

MY_STRUCT s;
s.a = 10;
PMYSTRUCT ps = &s;
ps->a = 20;

In c, typedef has a storage class semantics, just like static , auto and extern . 在c中, typedef具有存储类语义,就像staticautoextern

Consider this: 考虑一下:

static int a, *p; - declares a to be a static variable of type int , and p to be a static variable of type pointer to int . -声明a为int类型的静态变量,而p为类型为int指针的静态变量。

typedef int a, *p - declares a to be the type int , and p to be a type pointer to int . typedef int a, *p声明a为int类型,p为类型为int的指针

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

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