zoukankan      html  css  js  c++  java
  • c语言之使用typedef定义类型

    可以用typedef声明新的类型名来代替已有的类型名。

    实例1:

    #include<stdio.h>
    #include<iostream>
    typedef struct {
        char* name;
        int age;
    }STUDENT;
    
    int main()
    {
        STUDENT stu;
        stu.name = "tom";
        stu.age = 12;
        printf("name=%s,age=%d
    ", stu.name, stu.age);
        system("pause");
        return 0;
    }

    实例2:

    #include<stdio.h>
    #include<iostream>
    typedef int NUM[100];
    
    int main()
    {
        NUM num = {0};
        printf("%d
    ", sizeof(num));
        system("pause");
        return 0;
    }

    输出:

    正好是400个字节 ,因为一个整型占4个字节,共100个元素。

    实例3:

    #include<stdio.h>
    #include<iostream>
    typedef char* STRING;
    
    int main()
    {
        STRING str = "hello";
        printf("%s
    ", str);
        system("pause");
        return 0;
    }

    输出:

    我们就可以自己定义string类型了。

    实例4:

    #include<stdio.h>
    #include<iostream>
    typedef int (*POINTER)(int,int);
    
    int add(int a, int b) {
        return a + b;
    }
    int main()
    {
        int add(int, int);
        POINTER  p;
        p = add;
        int res = p(2, 3);
        printf("%d
    ", res);
        system("pause");
        return 0;
    }

    输出:

    这样我们也可以定义函数指针。 

  • 相关阅读:
    Execution Context(EC) in ECMAScript
    Prototype Chain
    一次websocket的抓包体验
    nodejs 解析 base64 文本
    curl常用命令行总结
    nodejs stream基础知识
    typedarrays splice
    nodejs stream & buffer 互相转换
    nodejs buffer 总结
    ajax stream 一边下载二进制数据一边处理
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12129210.html
Copyright © 2011-2022 走看看