-
Notifications
You must be signed in to change notification settings - Fork 1
/
结构体.c
62 lines (47 loc) · 1.2 KB
/
结构体.c
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
57
58
59
60
61
62
#include <stdio.h>
//定义一个用户结构体,结构体类型名User
struct User
{
char *name;
int age;
int height;
char sex;
}ourUser; //定义一个User结构体的时候指定一个变量
//通过结构体指针修改一个结构体变量的数据
void modifyUser(struct User *p){
printf("sizeof(user)=%d\n",sizeof(p));
p->name="Bob";
p->age=1;
// (*p) ==> user
(*p).height=50;
(*p).sex='X';
}
int main(int argc, char *argv[])
{
struct User user;
printf("sizeof(user)=%d\n",sizeof(user));
user.name="麦当劳";
user.age=63;
user.height=10;
user.sex='M';
modifyUser(&user);
//ourUser={"kdj",66,15,'M'};
ourUser.name="kdj";
ourUser.age=66;
struct
{
char *name;
int age;
}stu; //直接定义结构体变量,不指定类型名
stu.name="Mike";
stu.age=30;
struct User myUser[2]={{"Mike",25,175,'M'},{"Jenny",24,170,'F'}};
printf("name=%s age=%d\n",ourUser.name,ourUser.age);
printf("user.name=%s \t user.age=%d \t user.height=%d \t user.sex=%c \n",user.name,user.age,user.height,user.sex);
printf("name=%s age=%d\n",stu.name,stu.age);
for(int u=0;u<2;u++){
printf("myUser[%d].name=%s myUser[%d].age=%d myUser[%d].height=%d myUser[%d].sex=%c\n",
u,myUser[u].name,u,myUser[u].age,u,myUser[u].height,u,myUser[u].sex);
}
return 0;
}