Linux及C语言高级[7](C语言结构体)

“这是我参与8月更文挑战的第7天,活动详情查看:8月更文挑战

1结构体

  • 它属于构造类型,结构体中的成员在内存上也是连续的,但是结构体中的成员类型可以相同,也可以不相同。
  • 定义格式:
struct student{
    char name[20] ;
    int age;
    char sex;
};
复制代码
  • 使用结构体定义变量

struct student p1,p3; // int p1;

struct student *p2 = &p1; // int *p2 = &p1;

  • 对结构体变量中的成员赋值
strcpy(p1.name ,"zhangsan");
p1.age  = 25;
p1.sex  = 'm';

strcpy(p2->name ,"zhangsan");
p2->age  = 25;
p2->sex  = 'm';
复制代码
  • struct :是结构体的关键字
  • student:类型名
  • {} :内部的内容就是结构体的成员
  • ; :结构体的结尾需要加;
  • 成员类型:结构体中成员类型可以不相同,可以是任意的类型
  • struct student p1; :相当于使用了struct student类型定义了一个p1的变量
  • . :通过结构体变量访问结构体中的成员
  • -> :如果是结构体指针变量通过->来访问里面的成员

2结构体数组

struct student p1;      //int p1;
struct student p1[2];   //int p1[2];
	
p1[0].name
p1[0].age
p1[0].sex
	
p1[1].name
p1[1].age
p1[1].sex
复制代码

3结构体的使用

3.1定义结构体的同时进行变量的定义

struct student{
    char name[20] ;
    int age;
    char sex; 
}p1,p2[2],*p3;  
复制代码

定义了p1的变量,p2[2]数组,p3指针。

3.2无名结构体

struct {
    char name[20] ;
    int age;
    char sex;
}p1,p2[2],*p3;	
复制代码

定义了p1的变量,p2[2]数组,p3指针。不能使用结构体类型在定义其他的变量。

3.3结构体跟typedef结合

typedef struct student{
    char name[20] ;
    int age;
    char sex;
}stu_t;  //类型
复制代码

stu_t p1; //定义了p1的变量
stu_t *p2; //定义了p2的指针

typedef struct {
    char name[20] ;
    int age;
    char sex;
}stu_t; //类型	
复制代码

stu_t p1; //定义了p1的变量
stu_t *p2; //定义了p2的指针

3.4结构体变量的赋值

3.4.1先定义后赋值

struct student{
    char name[20] ;
    int age;
    char sex; 
};  
			
struct student p1;
			
strcpy(p1.name,"zhangsan");
p1.age = 20;
p1.sex = 'm';
复制代码

3.4.2定义的同时赋值

struct student{
    char name[20] ;
    int age;
    char sex; 
};  
			
struct student p1 = {
    "zhangsan",
    20,
    'm'
};
复制代码

3.4.3通过.的形式赋值

struct student{
    char name[20] ;
    int age;
    char sex; 
};  
			
struct student p1 = {
    .name = "zhangsan",
    .sex = 'm'
};
复制代码

可以指定成员来赋值,不想赋值的成员,可以直接忽略

3.4.4定义类型同时定义变量并赋值

struct student{
    char name[20] ;
    int age;
    char sex; 
}p1 = {
    "zhangsan",
    20,
    'm'
};  
复制代码

3.4.5定义类型同时定义变量并跨成员赋值

struct student{
    char name[20] ;
    int age;
    char sex; 
}p1 = {
    .name = "zhangsan",
    .sex = 'm'
}; 
复制代码

3.5结构体数组的赋值

3.5.1先定义后赋值

struct student p1[2];   //int p1[2];
		
p1[0].name
p1[0].age
p1[0].sex
		
p1[1].name
p1[1].age
p1[1].sex
复制代码

3.5.2定义的同时赋值

struct student p1[2] = {
    {"zhangsan",20,'m'},
    {"lisi",30,'w'},
};
struct student p1[2] = {
    {
    .name = "zhangsan",
    .age  = 20,
    .sex  = 'm'
    },
    {
    .name = "lisi",
    .sex  = 'w'
    },
};
复制代码

3.5.3定义的同时跳过成员赋值

struct student p1[3] = {
    [0] = {
    .name = "zhangsan",
    .age  = 20,
    .sex  = 'm'				
    },
    [2] = {
    .name = "lisi",
    .sex  = 'w'
    },
};
复制代码

4结构体的嵌套

struct aa{
    int a;
    int b;
    int c;
};
typedef struct{
    char name[20] ;
    int age;
    char sex; 
    int score;
    struct aa t;
}stu_t;
		
typedef struct{
    stu_t stu[30];
    int n;   //当前班级内学生的个数
}class_t;
	
class_t cls;
cls.stu[0].name
cls.stu[0].age
cls.stu[0].sex
cls.stu[0].score
cls.stu[0].t.a = 10;
cls.stu[0].t.b = 10;
cls.stu[0].t.c = 10;
		...
cls.stu[29]
	
cls.n
复制代码

5结构体和函数指针的结合

结构体内部不能够写函数,但是可以写函数指针。原因:函数所占的内存的大小不确定。

struct operations{
    int (*open)(void);
    int (*read)(void);
    int (*write)(void);
    int (*close)(void);
};
复制代码

6结构体指针数组,结构体数组指针

6.1结构体指针数组

typedef struct{
    char name[20] ;
    int age;
    char sex; 
    int score;
    struct aa t;
}stu_t;
	
stu_t *p[3];  //结构体指针数组
p[0] = malloc(sizeof(stu_t));
p[1] = malloc(sizeof(stu_t));
p[2] = malloc(sizeof(stu_t));
复制代码

6.2结构体数组指针

stu_t (*p)[3]; //它是一个指针,指向一行。
stu_t arr[4][3];
p = arr;
复制代码

7结构体占用的内存大小

? 1.如果结构体内部的成员小于等于4字节,就按照最大的成员对齐。

2.如果结构体内部成员有大于4字节的成员,全部按照四字节对齐

3.如果在结构体内部有char short占用4字节,第二个成员总是从偶数的位置开始存储

8共用体(联合体)

  • 共用体:它属于构造类型,在共用体内部所有的成员共用最大成员所占用的字节数

  • 定义格式:

union aa{
    char a;
    short b;
    int c;
    double d;
};
复制代码
  • 定义变量:

union aa a;

union aa *b;

  • 赋初值:

a.a = 'c';
或者
a.d = 3.1415926;

b = malloc(sizeof(*b));

b->d = 3.1415925;

  • 联合体的作用?(节省空间)
struct class{
    struct person p[30];
    int n;
};
	
struct person{
    char name[20];
    int age;
    char sex;
union a{
    int score;
    int salar;
    }b;			
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享