Using typedef defined type of language c

Typedef can be used to declare a new type name to replace the existing type name.

Example 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\n", stu.name, stu.age);
    system("pause");
    return 0;
}

Example 2:

#include<stdio.h>
#include<iostream>
typedef int NUM[100];

int main ()
{
    IN a = {0};
    printf("%d\n", sizeof(num));
    system("pause");
    return 0;
}

Output:

 

Exactly 400 bytes, as an integer is four bytes, a total of 100 elements.

Example 3:

#include<stdio.h>
#include<iostream>
typedef char* STRING;

int main ()
{
    STRING str = "hello";
    printf("%s\n", str);
    system("pause");
    return 0;
}

Output:

 

We can define your own type of string.

Example 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\n", res);
    system("pause");
    return 0;
}

Output:

 

In this way we can define a function pointer. 

Guess you like

Origin www.cnblogs.com/xiximayou/p/12129210.html