C语言——结构体、动态存储分配、链表

结构体

引入结构体

问题定义:将不同类型的数据组合成一个有机整体
举例:学生(学号、姓名、性别、年龄、地址)等属性:

struct
{
	int num;
	char name[20];
	char sex;
	int age;
	char addr[30];
};

假设:

numnamesexageaddr
007JaneM20Beijing

定义结构的一般形式:

struct 结构名
{
	成员列表
};
  • 先声明结构类型在定义变量名
struct 结构名
{
	成员列表
};
struct student 变量名列表;

在定义结构体变量名后,系统会自动分配内存
(4+20+1+4+30=59)
在这里,你去用自己电脑去试一下下边这个代码,看是否为59

#include <stdio.h>
struct student
{
	int num;
	char name[20];
	char sex;
	int age;
	char addr[30];
}student1, student2;

int main(void)
{
	printf("%d\n",sizeof(struct student));
	return 0;
}

结果不同是由于编辑器的优化结果,以及内存对齐

  • 在声明类型的同时定义变量
struct 结构体名
{
	成员列表
}变量名列表;
  • 直接定义结构体类型变量
struct 
{
	成员列表
}变量名列表;

举例如下:

Numnamesexagebirthdayaddr
month/day/year
struct data
{
	int month;
	int day;
	int year;
};
struct boy
{
	int num;
	char name[30];
	char sex;
	int age
	strcuct data birthday;
	chat addr[30];
}boy1, boy2;

结构体变量的引用

结构体变量名.成员名
“ . " 是优先级最大的运算符

  • 可以引用结构体变量的地址,也可以引用结构体变量名的的地址
  • 结构体变量成员与普通变量成员一样,可以进行各种运算

结构体变量的初始化

struct student
{
	int num;
	char name[20];
	char sex;
	int age;
	char addr[30];
}student1 = {01, "jann", 'm', "brijin"};

结构体数组

直接代码展示:

struct person
{
	char name[20];
	char phone[15];
};

int main(void)
{
	struct person people[10];
	int i;
	for(i = 0; i < 10; i ++)
	{
		gets(people[i].name);
		gets(people[i].phone);
	}
	for(i = 0; i < 10; i ++)
	{
		puts(people[i].name);
		puts(people[i].phone);
	}
	return 0;
}
	

指向结构体类型数据的指针

  • 结构体指针变量说明的一般形式为:
    struct 结构名 *结构指针变量名
struct student *pstu;
  • 赋值是把结构变量的首地址赋予给指针变量,不能把结构名赋予指针变量
struct student
{
	....
}student1;
struct student *pstu;
pstu = &student1; // right
pstu = &student; // wrong
  • 访问的一般形式:
    (*结构指针变量).成员名 or 结构指针变量->成员名

    例如:

(*pstu).num;
pstu->num;

结构指针变量作函数参数

将一个结构体变量的值传递给另一个函数,有一下方法:
  • 用结构体变量的成员作参数
  • 用结构体变量作实参
  • 用指向结构体变量(或数组)的指针作实参,将结构体变量(或数组)的地址传给形参

实战举例:

  • 通过调用函数print,输出结构体变量内的内容
#include <stdio.h>
#include <string.h>

struct student
{
	int num;
	char name[20];
	double score;
};

void print(struct student);

int main(void)
{
	struct student stu;          // 定义结构体变量名
	
	stu.num = 01;
	strcpy(stu.name, "Danny");
	stu.score = 90.001;

	print(stu);       // 传参
}

void print(struct student stu) 
{
	printf("num\t:%d\n", stu.num);
	printf("name\t:%s\n", stu.name);
	printf("score\t:%llf\n", stu.score);
}	
  • 第二种
#include <stdio.h>
#include <string.h>

struct student
{
	int num;
	char name[20];
	double score;
};

void print(struct student *);

int main(void)
{
	struct student stu;          // 定义结构体变量名
	
	stu.num = 01;
	strcpy(stu.name, "Danny");
	stu.score = 90.001;

	print(&stu);       // 传参
}

void print(struct student *p) 
{
	printf("num\t:%d\n", stu.num);
	printf("name\t:%s\n", stu.name);
	printf("score\t:%llf\n", stu.score);
}	

动态存储分配

常用的内存管理函数:

  • 分配内存空间函数 malloc、 calloc
  • 释放内存空间函数 free
malloc 函数
  • 函数原型 void *malloc(unsigned int size);
  • 作用:在内存的动态存储区中分配一个长度为 size 的连续空间
  • 函数返回值是一个指向分配域起始地址的指针(类型为void)
  • 如果函数未成功执行,则返回NULL
calloc函数
  • 函数原型为 void *calloc(unsigned n, unsigned size)
  • 作用是在内存的动态存储区中分配 n 个长度为 size 的连续空间
  • 函数返回一个指针分配域起始地址的指针
free函数
  • 函数原型 void free(void *p)
  • 作用是释放由 p 指向的内存区,使这部分内存区能被其它变量使用
  • free函数无返回值

链表

链表是一种常见的重要的数据结构,是动态地进行存储分配的一种结构

链表我将会单独写一篇博客来学习

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值