读书笔记: C结构体(structure)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#define MAXSIZE 10

#define NULL 0
#define TYPE struct line_stu
#define LEN sizeof(struct line_stu)

#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))

struct stu {
    int num;
    char *name;
    char sex;
    float score;
} boy1,boy2;

struct line_stu{
    int num;
    int age;
    struct line_stu *next;
};

TYPE *create(int n){
    struct line_stu *head,*pf,*pb;
    int i;
    for(i=0;i<n;i++){
        pb=(TYPE*) malloc(LEN);
        printf("input Number and Age\n");
        scanf("%d%d",&pb->num,&pb->age);
    }
    if(i==0)
        pf=head=pb;
    else
        pf->next=pb;
    pb->next=NULL; pf=pb;
    return head;
}

int test_arr(int arr[]){
    printf("%d \n",arr[1]);
    printf("%d\n",*(arr+1));
    return 0;
}

void use_boy_struct(){
    struct stu boy3;
    boy3.num=123;

    boy1.num=102;
    boy1.name="Zhang ping";
    printf("input sex and score\n");
    scanf("%c %f",&boy1.sex,&boy1.score);
    boy2=boy1;
    printf("Number=%d\nName=%s\n",boy2.num,boy2.name);
    printf("Sex=%c\nScore=%f\n",boy2.sex,boy2.score);
}