初识结构体

201 阅读4分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第1天,点击查看活动详情

结构体

什么是结构体?

在之前的学习中,我们已经了解到C语言提供了基本数据类型,例如 char 、short 、int 、float…等类型,那么怎样设计出来属于自己的类型呢?

这就要用到结构体了,C 语言允许用户自己指定这样一种数据结构,它由不同类型的数据组合成一个整体,以便引用,这些组合在一个整体中的数据是互相联系的,这样的数据结构称为结构体。

定义形式

struct 结构体名{
    结构体所包含的变量或数组
};

举个例子,下面就是一个包含学生信息的结构体

struct student{
    char name[20];  //姓名
    long long id;  //学号
    int age;  //年龄
    float score;  //成绩
};

定义结构体类型变量的方法

1. 先声明结构体类型再定义变量名

还是拿上面的例子来说

#include <stdio.h>
struct student{
    char name[20]; 
    long long id; 
    int age; 
    float score;  
};//先声明
//注意大括号外面也有一个分号!

int main(){
	struct student st1;//再定义
	st1.age = 18;//结构体中变量的访问
	printf("%d",st1.age);//18
	return 0;
}

2. 在声明类型的同时定义变量

#include <stdio.h>
struct student{
   char name[20]; 
   long long id; 
   int age; 
   float score;  
}st1;//声明的同时定义

int main(){
   st1.age = 18;
   printf("%d",st1.age);
   return 0;
}

3. 直接定义结构体类型变量,省略类型名

#include <stdio.h>
struct {
    char name[20]; 
    long long id; 
    int age; 
    float score;  
}st1;//声明的同时定义

int main(){
	st1.age = 18;
	printf("%d",st1.age);
	return 0;
}

4. 使用typedef定义数据类型

关键字typedef用于为系统固有的或者程序员自定义的数据类型定义一个别名。数据类型的别名通常使用大写字母。

typedef int INTEGER;

这样就为int定义了一个新的名字INTEGER,int与INTEGER是一个意思,是完全等价的。

所以,我们当然也可以使用typedef来为结构体定义一个别名,让我们使用更加方便。

#include <stdio.h>

typedef struct student{
    char name[20]; 
    long long id; 
    int age; 
    float score; 
}STUDENT;//声明结构体的同时起一个别名,STUDENT就相当于struct student

int main(){
	STUDENT st1;
	
	st1.age = 18;
	printf("%d",st1.age);
	return 0;
}

或者

#include <stdio.h>

struct student{
    char name[20]; 
    long long id; 
    int age; 
    float score; 
};//先声明
typedef struct student STUDENT;//再起别名

int main(){
	STUDENT st1;
	st1.age = 18;
	printf("%d",st1.age);
	return 0;
}

结构体的访问

访问格式:结构体变量名+.+数据变量名

#include <stdio.h>

struct student{
    char name[20]; 
    long long id; 
    int age; 
    float score; 
};
typedef struct student STUDENT;

int main(){
	STUDENT st1;
	st1.age = 18;//对st1中的age进行访问
	printf("%d",st1.age);
	return 0;
}

接下来我们就可以对结构体变量进行赋值了,赋值方式仍然与之前的基本数据类型一样。

#include <stdio.h>

struct student{
    char name[20]; 
    long long id; 
    int age; 
    char gender; 
};
typedef struct student STUDENT;

int main(){
	STUDENT st1;
	scanf("%s",st1.name);
	scanf("%d",&st1.age);
	st1.gender = 'w';
	printf("%s%d岁了",st1.name,st1.age);
	return 0;
}

那么,如果我们是用指针指向结构体时,该如何访问呢?

之前是.,用指针后就要变成->

#include <stdio.h>

struct student{
    char name[20]; 
    long long id; 
    int age; 
    char gender; 
};
typedef struct student STUDENT;

int main(){
	STUDENT *st1;//定义一个STUDENT类型的指针
	STUDENT stu;//定义一个STUDNET结构体
	st1 = &stu;//将是st1指针指向stu
	st1->id = 202231061201;//注意用的是->
	scanf("%s",st1->name);
	scanf("%d",&st1->age);
	st1->gender = 'w';
	printf("%s%d岁了\n",st1->name,st1->age);
	printf("%s%d岁了",(*st1).name,(*st1).age);//st1指针解引用之后就与普通结构体变量一样用.
	return 0;
}

结构体的初始化

除了常规的初始化外,结构体可以赋值进行初始化

#include <stdio.h>

struct student{
    char name[20];//姓名
    int age;//年龄
    char gender;//性别
    long long id;//学号
};
typedef struct student STUDENT;

int main(){
	STUDENT stu= {"李华",18,'w',202231061201};
	return 0;
}

结构体数组

用结构体数组,就可以记录多个学生的信息了

#include <stdio.h>

struct student{
    char name[20];//姓名
    int age;//年龄
    char gender;//性别
    long long id;//学号
};
typedef struct student STUDENT;

int main(){
	STUDENT stu[3] = {{"李华",18,'w',202231061201},
					{"小红",19,'m',202231061202},
					{"小明",20,'w',202231061203}};
	return 0;
}

结构体所占字节

#include <stdio.h>

typedef struct test{
	char ch;
	int a;
	short b;
}STU;


int main(){
	printf("%d",sizeof(STU));
	return 0;
}

运行一下试试看,你发现了什么?(内存对齐)