目录
结构体声明
结构体初始化
结构体访问
结构体传参
结构体声明?
结构体有自己的关键字struct
结构体声明:
struct Book
{
char name[20];
char author[15];
float price;
}b1,b2;
结构体初始化
typedef struct Stu
{
char name[20];
int age;
char id[20];
}Stu;
int main()
{
struct Stu s1 = {"张三",20,"20210605"};
Stu s2 = {"李四",15,"20210305"};
return 0;
}
结构体访问
struct S
{
int a;
char c;
double d;
};
struct T
{
struct S s;
char name[20];
int num;
};
int main()
{
struct T t = { { 100, 'c', 3.14 }, "李四", 30 };
printf("%d %c %f %s %d\n", t.s.a, t.s.c, t.s.d, t.name, t.num);
struct T* pt = &t;
//printf("%d %c %f %s %d\n", (*pt).s.a);
printf("%d %c %f %s %d\n", pt->s.a, pt->s.c, pt->s.d, pt->name, pt->num);
return 0;
}
结构体传参
struct S
{
int arr[10];
int num;
char ch;
double d;
};
void print1(struct S ss)
{
printf("%d %d %d %d %c %lf\n", ss.arr[0], ss.arr[1], ss.arr[2], ss.num, ss.ch, ss.d);
}
void print2(struct S* ps)
{
printf("%d %d %d %d %c %lf\n", ps->arr[0], ps->arr[1], ps->arr[2], ps->num, ps->ch, ps->d);
}
int main()
{
struct S s = { { 1, 2, 3, 4, 5 }, 100, 'w', 3.14 };
print1(s);
print2(&s);
return 0;
}
|