define和typedef的區別

小石王發表於2018-06-09

define和typedef的區別

define是單純的字元替換,typedef是重新定義了新的型別

#include <stdio.h>

#define CHAR1 char*

typedef char* CHAR2;

int main(){
  char c = `a`;

  CHAR1 pc1,pc2;//pc1是char指標,但pc2是char
  pc1 = &c;
  pc2 = c;
  *pc1 = `w`;
  printf("%c ", c);

  CHAR2 pc3,pc4;//pc3,pc4都是char指標
  pc3 = &c;
  pc4 = &c;
  *pc4 = `e`;
  printf("%c ", c);
}

相關文章