typedef

作者: qianranow | 来源:发表于2018-06-14 10:38 被阅读38次

    0. 理解


    • 作用:给一个 已知数据类型 起别名

    1. 基本数据类型


    typedef int Integer;
    int main() {
      Integer num = 100;
      printf("%i\n", num);
      return 0;
    }
    

    2. 指针


    typedef char *String;
    int main() {
      String name = "王小熊";
      printf("name = %s\n", name);
      return 0;
    }
    
    int sum(int v1, int v2) {
      return v1 + v2;
    }
    int minus(int v1, int v2) {
      return v1 - v2;
    }
    typedef int(*functionPotinter)(int , int);
    int main() {
      functionPotinter sumP = sum;
      printf("sum = %i\n", sumP(20, 10));
      functionPotinter minusP = minus;
      printf("minus = %i\n", minusP(20, 10));
      return 0;
    }
    

    3. 结构体


    struct Person {
      char *name;
      int age;
      double height;
    };
    typedef struct Person SPerson;
    int main() {
      SPerson sp = {"秦子阳", 18, 1.88};
      printf("%s\n", sp.name);
      return 0;
    }
    
    typedef struct Person {
      char *name;
      int age;
      double height;
    } Sperson;
    int main() {
      Sperson sp = {"秦子阳", 18, 1.88};
      printf("%s\n", sp.name);
      return 0;
    }
    
    typedef struct {
      char *name;
      int age;
      double height;
    } Sperson;
    int main() {
      Sperson sp = {"秦子阳", 18, 1.88};
      printf("%s\n", sp.name);
      return 0;
    }
    

    4. 枚举


    enum Gender {
      kGenderMale,
      kGenderFemale
    };
    typedef enum Gender SEX;
    int main() {
      SEX sex = kGenderFemale;
      printf("%i\n", sex);
      return 0;
    }
    
    typedef enum Gender {
      kGenderMale,
      kGenderFemale
    } SEX;
    int main() {
      SEX sex = kGenderFemale;
      printf("%i\n", sex);
      return 0;
    }
    
    typedef enum {
      kGenderMale,
      kGenderFemale
    } SEX;
    int main() {
      SEX sex = kGenderFemale;
      printf("%i\n", sex);
      return 0;
    }
    

    相关文章

      网友评论

          本文标题:typedef

          本文链接:https://www.haomeiwen.com/subject/jrveeftx.html