struct的声明和面向对象中的类还是有少许区别,现总结struct的声明的用法以备以后复习。

1 正规写法,这样便声明了一个类型struct apple:

struct apple{
...
};


2 同时声明一个此类型的变量,这样不但有了struct apple这个变量类型,还同时声明了一个变量myApple:

struct apple{
...
}myApple;


3 不加变量类型名,只是声明一个变量:

struct {
...
}myApple;

4  使用typedef,将struct apple进行重新定义类型:

typedef struct apple{
...
}apple;
apple myApple;

上面的struct apple 和 apple都是指的同一个结构体类型,这里并用apple类型定义了一个变量myApple。当然这里的struct apple处的apple也可以不要。

union声明和定义变量的用法和struct是相似的。

5 enum声明很形象,并且c中还可以赋值给int类型。

enum msgtype {
HARD_INT = 1,
/* SYS task */
GET_TICKS,
DEV_OPEN = 1001,
};

使用的时候直接使用就可以,比如:

int type = GET_TICKS;




相关文章:

  • 2021-10-01
  • 2021-12-13
  • 2022-02-03
  • 2022-12-23
  • 2021-10-06
  • 2022-01-26
  • 2021-11-27
猜你喜欢
  • 2022-02-12
  • 2021-10-01
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2021-07-24
相关资源
相似解决方案