Data structures created by users in C language

Data structures created by users in C language

Declaration and use of structures

Structure declaration
struct 结构体名{
    
      
   ...  
   成员表列  
   ...
}变量名表列
How to use structures

struct structure name variable name = {member table column};
You can use variable name.member name to access a certain data.
You can also use structure pointer->member name to access a certain data
structure pointer variable p. Use p->field Or (*p).field points to the content in the structure.
The structure variable p uses p.field to point to the content in the structure.

Examples of using structure variables
#include <stdio.h>
#include <string.h>
//根据成绩高低输出结构体
struct student{
    
    
    int id;
    char name[20];
    float score;
};

int main(){
    
    
    //定义结构体变量student1,student2,temp
    struct student student1,student2,temp;
    //定义结构体指针student3指向student1
    struct student *student3 = &student1;
    //输入id name 和score
    //结构体指针用p->field或(*p).field 结构体用p.field指向结构体里面的内容
    //scanf("%d%s%f",&student3->id,student3->name,&student3->score);
    scanf("%d%s%f",&(*student3).id,(*student3).name,&(*student3).score);
    scanf("%d%s%f",&student2.id,student2.name,&student2.score);
    /**
    交换算法 也可以用memcpy方法
    memcpy(&temp,&student1,sizeof(student1));
    memcpy(&student1,&student2,sizeof(student2));
    memcpy(&student2,&temp,sizeof(temp));
    **/
    if(student1.score<student2.score){
    
    
        temp = student1;
        student1 = student2;
        student2 = temp;
    }
    //按大小交换排序后 输出两个结构体的详细内容
    printf("id=%d name=%s score=%.2f\n",student1.id,student1.name,student1.score);
    printf("id=%d name=%s score=%.2f",student2.id,student2.name,student2.score);
	return 0;
}

Declaration and use of structure arrays

Declaration of structure array
struct student
{
    
    
   int id;
   char name[20];
   float score;
};
struct student stu[6]= {
    
    {
    
    1001,"LI MING",86},{
    
    1002,"LI HUA",80},{
    
    1003,"LIU MING",79},
       {
    
    1004,"ZHANG MING",85},{
    
    1005,"WANG MING",90},{
    
    1006,"LI LIN",87}};

The actual usage method is the same as the usage of arrays.
For example: if you need to use the score variable of the first member, use stu[0].score to access it.
The copy of the structure can be copied directly using the memcpy function.

Use of structure arrays
#include <stdio.h>
#include <string.h>
//根据成绩高低输出学生结构体
struct student
{
    
    
    int id;
    char name[20];
    float score;
};

int main()
{
    
    
    //定义结构体数组stu
    struct student stu[6]= {
    
    {
    
    1001,"LI MING",86},{
    
    1002,"LI HUA",80},{
    
    1003,"LIU MING",79},
        {
    
    1004,"ZHANG MING",85},{
    
    1005,"WANG MING",90},{
    
    1006,"LI LIN",87}
    };
    struct student temp;
    int i,j,k;
    //冒泡排序
    for( i = 0 ; i < 4; i ++)
    {
    
    
        k = i;
        for(j=i+1; j<5; j++)
        {
    
    
            //如果后者的分数比前者高
            if(stu[k].score<stu[j].score)
            {
    
    
                k = j;
            }
        }
        if(k != i)
        {
    
    
            //交换两个结构体
            temp = stu[i];
            stu[i] = stu[k];
            stu[k] = temp;
        }
    }
    //按分数从大到小输出
    for( i = 0 ; i < 6; i ++)
    {
    
    
        printf("id=%d name=%s score=%.2f\n",stu[i].id,stu[i].name,stu[i].score);
    }
	return 0;
}

Declaration and use of structure pointers

Declaration of structure pointer

A structure pointer is a pointer variable pointing to a structure variable. Its
usage and characteristics are the same as those of a normal array pointer.
The structure pointer variable p uses p->field or (*p).field to point to the content in the structure.

struct student{
    
    
    int id;
    char name[20];
    float score;
};
struct student stu[6]= {
    
    {
    
    1001,"LI MING",86},{
    
    1002,"LI HUA",80},{
    
    1003,"LIU MING",79},
        {
    
    1004,"ZHANG MING",85},{
    
    1005,"WANG MING",90},{
    
    1006,"LI LIN",87}
    };
struct student *temp=stu;
Use of structure pointers
#include <stdio.h>
#include <string.h>
//根据成绩高低输出结构体
struct student{
    
    
    int id;
    char name[20];
    float score;
};
//定义一个结构体数组
struct student stu[6]= {
    
    {
    
    1001,"LI MING",86},{
    
    1002,"LI HUA",80},{
    
    1003,"LIU MING",79},
        {
    
    1004,"ZHANG MING",85},{
    
    1005,"WANG MING",90},{
    
    1006,"LI LIN",87}
    };
int main(){
    
    
    //用结构体指针去循环输出结构体数组中的全部内容 p->field等价于(*p).field
    for(struct student *temp=stu ;temp<stu+6;temp++){
    
    
        //printf("id=%d name=%s score=%.2f\n",temp->id,temp->name,temp->score);
        printf("id=%d name=%s score=%.2f\n",(*temp).id,(*temp).name,(*temp).score);
    }
	return 0;
}

Linked lists and related applications

Single linked list and related applications

Single linked list : It is essentially a structure containing a structure pointer.
This structure pointer points to NULL when there is no successor node.
Once there is a successor node, the structure pointer points to the first address of the successor node. The
singly linked list determines that the end of the queue is that The pointer of the node pointing to the successor node points to NULL. This is the sign that the end of the queue has been reached. At this time, p->next=NULL. The
following code is the basic usage of a singly linked list, including the declaration/exchange/traversal of singly linked list nodes.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//单链表及相关应用
//定义单链表
struct student{
    
    
    int id;
    char name[20];
    float score;
    struct student *next ;
};
char *str[] = {
    
    "1st" , "2nd" , "3rd"};
int main(){
    
    
    //先初始化第一个节点 next值为空
    struct student stu1 = {
    
    1001,"LI MING",86,NULL};

    printf("1st node is id=%d name=%s score=%.2f\n",stu1.id,stu1.name,stu1.score);
    //初始化第二个节点 next值为空 使用malloc 动态分配内存给temp指向的结构体变量
    struct student *temp =(struct student *) malloc(sizeof(struct student));
    temp->id=1002;
    strcpy(temp->name,"LI HUA");
    temp->score=95;
    temp->next=NULL;
    //第一个节点的next值设置为第二个节点
    stu1.next=temp;
    printf("2nd node is id=%d name=%s score=%.2f\n",stu1.next->id,stu1.next->name,stu1.next->score);
    //初始化第三个节点 next值为空
    struct student *temp1 =(struct student *) malloc(sizeof(struct student));
    temp1->id=1003;
    strcpy(temp1->name,"LIU QIANG");
    temp1->score=90;
    temp1->next=NULL;
    //将第一个节点的next指向的第二个节点的next值设置为第二个节点
    stu1.next->next=temp1;
    printf("3rd node is id=%d name=%s score=%.2f\n",stu1.next->next->id,stu1.next->next->name,stu1.next->next->score);
    //交换第二个节点和第三个节点
    //保存头节点地址
    struct student head = stu1;
    //存储两个节点的值
    //拿temp2储存一个节点
    struct student *temp2 =(struct student *) malloc(sizeof(struct student));
    temp2 = head.next;
    head.next = head.next->next;
    head.next->next=temp2;
    head.next->next->next=NULL;
    //遍历链表
    struct student *p;
    int i=0;
    //如果p指向NULL那就表示遍历结束 到达链表尾部
    for(p=&head;p!=NULL;p=p->next,i++){
    
    
        printf("%s node is id=%d name=%s score=%.2f\n",str[i],p->id,p->name,p->score);
    }
	return 0;
}

To exchange two nodes, you need to modify the pointing of the next pointers of the two nodes according to the above code and also use an intermediate variable to save the content of a node.

Double linked list and related applications

Double-linked list : It is essentially a structure containing two structure pointers.
One structure pointer head points to the first address of the previous node,
and the other structure pointer next points to the first address of the next node.
The double-linked list determines that the head of the queue is the node. The pointer pointing to the previous node points to NULL, which is the sign of reaching the head of the queue. At this time, p->head=NULL, the
double linked list determines that the tail of the queue is the node. The pointer pointing to the subsequent node points to NULL, which is the sign of reaching the end of the queue. At this time, p->next=NULL
, the following code is the basic usage of doubly linked list, including the declaration/exchange/traversal of doubly linked list nodes.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//双链表及相关应用
//定义双向链表
struct student{
    
    
    int id;
    char name[20];
    float score;
    struct student *next,*head;
};
char *str[] = {
    
    "1st" , "2nd" , "3rd"};

int main(){
    
    
    //先初始化第一个节点 next值为空
    struct student stu1 = {
    
    1001,"LI MING",86,NULL,NULL};
    printf("1st node is id=%d name=%s score=%.2f\n",stu1.id,stu1.name,stu1.score);
    //初始化第二个节点 next值为空 head值也为空
    struct student *temp =(struct student *) malloc(sizeof(struct student));
    int i=0;
    temp->id=1002;
    strcpy(temp->name,"LI HUA");
    temp->score=95;
    temp->next=NULL;
    temp->head=NULL;
    //第一个节点的next值设置为第二个节点
    stu1.next=temp;
    stu1.next->head=&stu1;
    printf("2nd node is id=%d name=%s score=%.2f\n",stu1.next->id,stu1.next->name,stu1.next->score);
    //初始化第三个节点 next值为空 head值也为空
    struct student *temp1 =(struct student *) malloc(sizeof(struct student));
    temp1->id=1003;
    strcpy(temp1->name,"LIU QIANG");
    temp1->score=90;
    temp1->next=NULL;
    temp1->head=NULL;
    //将第一个节点的next指向的第二个节点的next值设置为第二个节点
    stu1.next->next=temp1;
    stu1.next->next->head=stu1.next;
    printf("3rd node is id=%d name=%s score=%.2f\n",stu1.next->next->id,stu1.next->next->name,stu1.next->next->score);
    //交换第二个结点和第三个结点
    //保存头节点地址
    struct student head = stu1;
    //存储两个节点的值
    //拿temp储存一个节点
    //依次交换head和next的值
    printf("swap 2nd node with 3rd node\n");
    struct student *temp2 =(struct student *) malloc(sizeof(struct student));
    temp2 = head.next;
    head.next = head.next->next;
    head.next->head=&head;
    head.next->next=temp2;
    head.next->next->head=head.next;
    head.next->next->next=NULL;
    //遍历链表
    struct student *p,*rare;
    //如果p指向NULL那就表示遍历结束 到达链表尾部
    printf("traversal from head to rear\n");
    for(i=0,p=&head;p!=NULL;p=p->next,i++){
    
    
        printf("%s node is id=%d name=%s score=%.2f\n",str[i],p->id,p->name,p->score);
        if(p->next==NULL){
    
    
            rare = p;
        }
    }
    printf("traversal from rear to head\n");
    for(i=0;rare!=NULL;rare=rare->head,i++){
    
    
        printf("%s node is id=%d name=%s score=%.2f\n",str[i],rare->id,rare->name,rare->score);
    }
	return 0;
}

To exchange two nodes, you need to modify the pointing of the next and head pointers of the two nodes according to the above code, and also use an intermediate variable to save the content of a node.

Circular linked lists and related applications

Circular linked list : essentially a structure containing a structure pointer.
This structure pointer generally points to the first address of the next node.
The difference between a circular linked list and a singly linked list is that the pointer field of the last node in the list points to the head. Nodes, the entire linked list forms a ring, so it is called a circular linked list.
The criterion for judging whether a circular linked list is empty is that head==head->next;the head node is equal to the successor node of the head node.
The following code is the basic usage of the circular linked list, including the declaration/exchange/traversal of the circular linked list nodes.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//循环链表及相关应用
//定义循环链表
struct student{
    
    
    int id;
    char name[20];
    float score;
    struct student *next;
};
char *str[] = {
    
    "1st" , "2nd" , "3rd"};
int main(){
    
    
    //先初始化第一个节点 next值为空
    struct student stu1 = {
    
    1001,"LI MING",86,NULL};
    struct student *headnode= &stu1;
    printf("1st node is id=%d name=%s score=%.2f\n",stu1.id,stu1.name,stu1.score);
    //初始化第二个节点 next值为空
    struct student *temp =(struct student *) malloc(sizeof(struct student));
    temp->id=1002;
    strcpy(temp->name,"LI HUA");
    temp->score=95;
    temp->next=NULL;
    //第一个节点的next值设置为第二个节点
    stu1.next=temp;
    stu1.next->next=&stu1;
    printf("2nd node is id=%d name=%s score=%.2f\n",stu1.next->id,stu1.next->name,stu1.next->score);
    //初始化第三个节点 next值为空
    struct student *temp1 =(struct student *) malloc(sizeof(struct student));
    temp1->id=1003;
    strcpy(temp1->name,"LIU QIANG");
    temp1->score=90;
    temp1->next=NULL;
    //将第一个节点的next指向的第二个节点的next值设置为第二个节点
    stu1.next->next=temp1;
    stu1.next->next->next=&stu1;
    printf("3rd node is id=%d name=%s score=%.2f\n",stu1.next->next->id,stu1.next->next->name,stu1.next->next->score);
    //交换第二个节点和第三个节点
    //保存头节点地址
    struct student head = stu1;
    //存储两个节点的值
    //拿temp储存一个节点
    struct student *temp2 =(struct student *) malloc(sizeof(struct student));
    temp2 = head.next;
    head.next = head.next->next;
    head.next->next=temp2;
    head.next->next->next=&head;
    //遍历循环链表
    struct student *p=&head;
    int i=0;
    //如果p的next指向循环链表的头那就表示遍历结束 即到达链表尾部
    do{
    
    
        printf("%s node is id=%d name=%s score=%.2f\n",str[i],p->id,p->name,p->score);
        p=p->next;
        i++;
    }
    while(p!=&head);
	return 0;
}
Summary of linked lists
  1. A linked list is an ordered string of structures containing structure pointers.
  2. Linked lists are mainly divided into three types: single linked list/double linked list/circular linked list
  3. Because singly linked lists are stored sequentially, singly linked lists are suitable for sequential search and sequential traversal.
  4. Because doubly linked lists are stored sequentially, doubly linked lists are also suitable for sequential searches and bidirectional traversals.
  5. Because circular linked lists are stored sequentially, circular linked lists are also suitable for sequential search and sequential traversal.
  6. Single linked lists and double linked lists are essentially the same. The only thing that needs attention is the modification of pointers when exchanging new nodes.
  7. The difference between a circular linked list and a single/double linked list is that the circular linked list is a ring structure, and the entire linked list is connected from head to tail to form a ring.

Stack and related applications

Basic properties of the stack
  1. As a data structure, the stack is a special linear list that can only perform insertion and deletion operations at one end. It stores data according to the last-in-first-out principle. The data that enters first is pushed to the bottom of the stack, and the last data is on the top of the stack. When data needs to be read, data is popped from the top of the stack (the last data is read out first).
  2. A stack is a special linear list that allows insertion and deletion operations at the same end. The end that allows insertion and deletion operations is called the top of the stack, and the other end is the bottom. The bottom of the stack is fixed, and the top of the stack floats. When the number of elements in the stack is zero, it is called an empty stack. Insertion is generally called PUSH, and deletion is called POP. Stack is also called first-in-last-out list.
  3. The stack has the property of first in, last out. The elements in the stack have the property of FILO (first in, last out).
  4. In computers, it is usually used to save program entries and parameters as well as temporary variables within functions.
  5. The stack has a size. The amount of data stored in the stack cannot exceed the number that can be stored in the stack. If it is exceeded, stack overflow will occur, causing the program and even the system to crash.
Basic application of stack

The following code is the basic usage of the stack, including the push function push() of the stack/the pop function pop()/the function top() that returns to the top of the stack/the function clear() that clears the stack.

#include <stdio.h>
//用数组模拟栈 FILO 先入后出

//元素elem进栈
int push(int* a,int top,int elem){
    
    
    a[++top]=elem;
    return top;
}
//数据元素出栈
int pop(int * a,int top){
    
    
    if (top==-1) {
    
    
        printf("空栈");
        return -1;
    }
    printf("出栈元素:%d\n",a[top]);
    top--;
    return top;
}
//tops方法返回栈顶元素
int tops(int * a,int top){
    
    
    if (top==-1) {
    
    
        printf("空栈");
        return -1;
    }
    return a[top];
}
int main() {
    
    
    int a[100];
    int top=-1;
    //通过push入栈 通过top查看入栈情况
    top=push(a, top, 1);
    printf("栈顶元素 %d\n",tops(a,top));
    top=push(a, top, 2);
    printf("栈顶元素 %d\n",tops(a,top));
    top=push(a, top, 3);
    printf("栈顶元素 %d\n",tops(a,top));
    top=push(a, top, 4);
    printf("栈顶元素 %d\n",tops(a,top));
    //通过pop函数 按先入后出的顺序弹出入栈的元素
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    return 0;
}

Queues and related applications

Basic properties of queues
  1. The queue is a special linear table. The special thing is that it only allows deletion operations at the front end of the table (front), and insertion operations at the back end (rear) of the table. Like the stack, the queue is an operation subject to Restricted linear table.
  2. The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the head of the queue.
  3. The queue has a first-in, first-out property. The elements in the queue have a FIFO (first-in, first-out) property.
  4. It is used to adjust the working conditions of different parts of the computer that read and write data. For example: a printer prints multiple files in the buffer in accordance with the printing sequence. The time slice rotation algorithm in the CPU scheduling algorithm is a variety of applications of queues in computers.
  5. The queue is a linear linked list of currently used and currently requested storage ranges, so there are no problems such as overflow.
Basic applications of queues

The following code is the basic usage of the queue, including the queue entry function push()/the queue exit function pop()/the return top function top() of the queue/the queue clearing function clear()

#include <stdio.h>
#include <string.h>
//用数组模拟队列 FLFO 先入先出

//入队 返回队列尾部位置
int push(int *a,int rear,int data){
    
    
    a[rear]=data;
    rear++;
    return rear;
}
//出队 返回目前的队列头位置
int pop(int *a,int front,int rear){
    
    
   if(front==rear){
    
    
    return 0;
   }
    printf("出队元素:%d\n",a[front]);
    return front+1;
}
//返回队顶元素
int top(int *a,int front,int rear){
    
    
    if(front==rear){
    
    
    return 0;
    }
    else{
    
    
        return a[front];
    }
}
void clear(int *a,int front,int rear){
    
    
    //如果 front==rear,表示队列为空
    while (front!=rear) {
    
    
        printf("出队元素:%d\n",a[front]);
        front++;
    }
    //全部初始化
    memset(a,'\0',100*sizeof(int));
}
int main() {
    
    
    int a[100];
    int front,rear;
    //设置队头指针和队尾指针,当队列中没有元素时,队头和队尾指向同一块地址
    front=rear=0;
    //入队
    rear=push(a, rear, 1);
    rear=push(a, rear, 2);
    rear=push(a, rear, 3);
    rear=push(a, rear, 4);
    //top 输出队列顶部元素
    top(a,front,rear);
    //出队方法1 pop 一次出一个
    front = pop(a,front,rear);
    front = pop(a,front,rear);
    //出队方法2 clear 一次出完
    clear(a, front, rear);
    return 0;
}

Declaration and use of union types

Union type declaration

Union type declaration

union 共用体名{
    
      
   ...  
   成员表列  
   ...
}变量表列

The usage of a union is the same as that of a structure, that
is, using the variable name of the union p p.filed points to a member variable in p.
The difference between a union and a structure is that
the union shares a memory space with the size of the memory occupied by the structure. The memory size of the largest member variable can be enabled only by the content of the last assignment before use. The previous assignment will be overwritten by subsequent assignments.
When the structure is declared, a memory space that can store all member variables is opened up. Assignment between each member and use without interfering with each other

Use of union types

The following code is the basic usage of the union type, including declaration/assignment/output and other operations of the union type.

#include <stdio.h>
//共用体公用一段内存空间 在使用之前的最后一次赋值的内容才能启用 之前赋值的没用
union data{
    
    
    char c;
    int i;
    float f;
    long long l;
};
int main(){
    
    
    union data d;
    d.c='b',d.i=97;
    //此时共用体内最后一次赋值为97 ASCII码对应a
    printf("c is %c and i is %d and f is %f\n",d.c,d.i,d.f);
    //此时共用体大小为最大的成员的大小 long long 64位 字节数为8
    printf("size of union is %d\n",sizeof(union data));
    d.c='g',d.i=101;
    //此时共用体内最后一次赋值为101 ASCII码对应e
    printf("c is %c and i is %d and f is %f\n",d.c,d.i,d.f);
    //此时共用体大小为最大的成员的大小 long long 64位 字节数为8
    printf("size of union is %d",sizeof(union data));
	return 0;
}

Declaration and use of enumeration types

Declaration of enumeration type

Declaration of enumeration type

enum 枚举名{
    
      
   枚举元素列表
}

For example, enum weekdays{mon,tue,wed,thu,fri,sat,sun};
you can define a series of legal enumeration type variables and assign values

 enum weekdays{
    
    mon,tue,wed,thu,fri,sat,sun};
 enum weekdays weekend,weekday;
 weekend = mon ;//mon是枚举类型中定义的枚举元素 赋值合法
 weekday = sunday ;//sunday不是枚举类型中定义的枚举元素 赋值不合法

Things to note about enumeration types:

  1. Enumerations can make the code clearer and more elegant, allowing for clear loops or traversals.
  2. Enumerations make code easier to maintain and can limit the range and range of values ​​that variables can use.
  3. The value of the enumeration type does not belong to any basic data type and cannot be directly output to the printf statement. It needs to be converted before it can be output.
Use of enumeration types

The following code is the basic usage of enumeration types, including operations such as declaration/assignment/output of enumeration types.

#include <stdio.h>
//枚举相关操作
//从星期六和星期天选一天休息日 和从星期一到星期五选一天非休息日 输出所有可能的排列
// 排列数C52 = 10 所以一共有十种排列
//声明枚举类型weekdays 储存非字符型数据 使用枚举类型主要方便遍历
enum weekdays{
    
    mon,tue,wed,thu,fri,sat,sun};
enum Num{
    
    };
int main(){
    
    
    //定义枚举类型变量
    enum weekdays weekend,weekday ,p;
    int n=0;
    //使用两个枚举类型变量 作为循环的相关参数
    for(weekend=sat;weekend<=sun;weekend++){
    
    
        for(weekday=mon;weekday<=fri;weekday++){
    
    
            if(weekend!=weekday){
    
    
                n++;
                printf("case %d ",n);
                for(int i=0;i<2;i++){
    
    
                    switch(i){
    
    
                        case 0:p=weekend,printf(" weekend is ");break;
                        case 1:p=weekday,printf(" weekday is ");break;
                        default:break;
                    }
                    //枚举类型数据不能直接输出,只能转化后再输出
                    switch(p){
    
    
                        case sun:printf("%s ","SUNDAY");break;
                        case mon:printf("%s ","MONDAY");break;
                        case tue:printf("%s ","TUESDAY");break;
                        case wed:printf("%s ","WEDNESDAY");break;
                        case thu:printf("%s ","THURSDAY");break;
                        case fri:printf("%s ","FRIDAY");break;
                        case sat:printf("%s ","SATURDAY");break;
                        default:break;
                    }
                }
                printf("\n");
            }
        }
    }
	return 0;
}

Use typedef to declare new type names

Definition of typedef

You can simply use typedef to declare a new type name to replace the original data type,
typedef char Line[81];Line text, secondline;
typedef char* PCHAR;PCHAR pa, pb;
or you can use typedef to replace a complex data type.

typedef struct tagPOINT
 {
    
    
    int x;
    int y;
}point;
 point p2 ={
    
    1,2};
 //而不用使用 struct tagPOINT p2 = {1,2};来定义
  1. Use typedef to avoid errors that may be encountered when declaring variables
  2. Using typedef only adds a type name to an existing type without creating a new data type.
  3. The effects of using typedef and #define macro definitions are similar but different. #define performs text replacement during pre-compilation, while typedef simply gives an alias to the data type.
Use of typedef

The following code is the basic usage of typedef

#include <stdio.h>
//typedef 关键字可以自行定义数据类型
//效果和#define类似 但是不是预编译时期进行文本替换
struct tagPOINT1
 {
    
    
    int x;
    int y;
};
//使用typedef修饰结构体 必须加上别名 不加就不通过编译
typedef struct tagPOINT
 {
    
    
    int x;
    int y;
}point;

int main(){
    
    
    typedef char* PCHAR;
    //char *pa,pb;可能会以为定义了pa pb两个字符指针 容易错误
    //PCHAR pa, pb;就不会出错
    PCHAR pa, pb;
    //正常情况初始化 结构体 需要多输一个struct
    struct tagPOINT1 p1 = {
    
    1,2};
    //使用typedef 每次可以减少一个struct
    point p2 ={
    
    1,2};
    printf("%d\n",sizeof(point));
    //避免重复定义复杂变量
    //不用每次都声明一个字符数组
    typedef char Line[81];
    Line text, secondline;
    gets(text);
    puts(text);
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/a695415974/article/details/122525176
Recomendado
Clasificación