Cyven's Braindump

typedef

  • belong: C
  • relation: TODO

typedef 的作用是起别名

typedef struct rect_t{
  int width;
  int length;
  int start_x;
  int start_y;
} my_rect;

在维护代码或者重构的时候,会方便

unsigned getRedPixel(int x,int y);
unsigned getGreenPixel(int x, int y);
unsigned getBluePixel(int x, int y);
int main(void){
  unsigned red,green,blue;
  ...
}

在上面的代码中, 因为语义 sementics 决定了像素值是 0-255, 而 unsigned 也就是 unsigned int 我们觉得太浪费空间了

unsigned char 比较适合

这个时候我们要一一去改就比较麻烦

如果使用搜索替换,可能有的地方它还是需要 unsigned 类型呢? 所以还是需要判断,不能全部替换

typedef unsigned rgb_t;
rgb_t getRedPixel(int x, int y);
...

如果一开始代码是这样的,那么更改起来就非常方便

typedef unsigned char rgb_t