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;
}

输出:

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

原文地址:https://www.cnblogs.com/xiximayou/p/12129210.html