C语言-结构体与共用体

本文介绍了C语言中结构体、共用体和动态内存分配的相关概念。首先讲解了结构体数组和指向结构体的指针,接着详细阐述了动态内存分配的函数malloc、calloc和free的使用。此外,还讨论了链表的建立、插入操作以及共用体与结构体的区别。最后,提到了枚举类型和用typedef定义新类型的方法。
摘要由CSDN通过智能技术生成

int 4字节;char 1字节;short 2字节;float 4字节;

1字节bytes=8位bits(2^8);

struct date
{
	int month;
	int day;
	int year;
}
struct
{
	int num;
	char name[20];
	char sex;
	struct date birthday;
	float score;
}boy1,boy2;
#include <stdio.h>
void main()
{
	struct date
	{
		int month;
		int day;
		int year;
	};
	struct
	{
		int num;
		char name[20];
		char sex;
		struct date birthday;
		float score;
	}boy1,boy2;

#include <stdio.h>
 
void main()
{
	struct student
	{
		int num;
		char *name;
		char sex;
		float score;
	} boy1, boy2;
 
	boy1.num = 007;
	boy1.name = "Jane";
 
	printf("Please input sex and score\n");
	scanf("%c %f", &boy1.sex, &boy1.score);
 
	boy2 = boy1;
 
	printf("Number = %d\nName = %s\n", boy2.num, boy2.name);
	printf("Sex = %c\nScore = %f\n", boy2.sex, boy2.score);
 
}
/*student1和student2在内存中各占?个字节*/
/*( 4 + 20 + 1 + 4 + 4 + 30 =  67 )*/
#include<stdio.h>
void main()
{
	struct student
	{
		int num;
		char name[20];
		char sex;
		int age;
		float score;
		char addr[30];
	}stu1,stu2;
	printf("%d\n",sizeof(stu1));
}
//实际为68

结构体数组:

一个通讯录: 

#include"stdio.h"
#define NUM 3
struct person
{
	char name[20];
	char phone[10];
};
void main()
{
	struct person man[NUM];
	int i;
	for(i=0;i<NUM;i++)
	{
		printf("input name:\n");
		gets(man[i].name);
		printf("input phone:\n");
		gets(man[i].phone);
	}
	printf("name\t\tphone\n\n");
	for(i=0;i<NUM;i++)
	{
		printf("%s\t\t%s\n\n",man[i].name,man[i].phone);
	}
}

例题:对候选人得票的统计程序。设有3个候选人,每次输入一个得票的候选人的名字,要求最后输出各人得票结果。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define NUM 4
struct person
{
	char name[20];
	int count;
}candidate[NUM]={
	{"A",0},
	{"B",0},
	{"C",0},
	{"D",0}
};
char *winner();
char *winner()
{
	int i=0,winner=i;
	for(;i<NUM;i++)
	{
		if(candidate[winner].count<candidate[i].count)
		{
			winner=i;
		}
	}
	return candidate[winner].name;
}
void main()
{
	int i,j;
	char candidate_name[20];
	printf("欢迎进入优秀毕业生投票系统:()\n\n");
	printf("候选人有:A B C D\n\n");
	for(i=1;i<=10;i++)
	{
		printf("第%2d位投票,请写下支持的候选人名字:",i);
		scanf("%s",candidate_name);
		for(j=0;j<NUM;j++)
		{
			if(0==strcmp(candidate_name,candidate[j].name))
			{
				candidate[j].count++;
			}
		}
	}
	printf("\n");
	for(i=0;i<4;i++)
	{
		printf("%s同学得票数为:%d\n",candidate[i].name,candidate[i].count);
	}
	printf("\n");
	printf("本次投票活动的胜利者是:%s",winner());
	printf("\n");
	system("pause");
}

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

一个结构体变量的指针就是该结构体变量所占据的内存段的起始地址。指针变量的值是结构体变量的起始地址,也可以用来指向结构体数组中的元素;与前面讨论的各类指针变量相同,结构指针变量也必须要先赋值后才能使用,是把结构变量的首地址赋予该指针变量,不能把结构名赋予该指针变量。因为,结构名只能表示一个结构形式,编译系统并不对它分配内存空间。只有当某变量被说明为这种类型的结构时,才对该变量分配存储空间。

struct  stu

{

       ..........

}boy;

pstu=&boy 

其访问的一般形式为:

(*结构指针变量).成员名    (*pstu).num
结构指针变量->成员名    pstu->num
#include<stdio.h>
struct stu
{
	int num;
	char *name;
	char sex;
	float score;
}boy1={102,"CSDN",'M',78.5};
void main()
{
	struct stu *pstu;
	pstu=&boy1;
	printf("Number = %d\nName = %s\n",boy1.num,boy1.name);
	printf("Sex = %c\nScore = %f\n\n",boy1.sex,boy1.score);
	printf("Number = %d\nName = %s\n",(*pstu).num,(*pstu).name);
	printf("Sex = %c\nScore = %f\n\n",(*pstu).sex,(*pstu).score);
	printf("Number = %d\nName = %s\n",pstu->num,pstu->name);
	printf("Sex = %c\nScore = %f\n\n",pstu->sex,pstu->score);
}

结构指针变量作函数参数:将一个结构体变量的值传递给另一个函数,有3个方法:

(1)用结构体变量的成员作参数

(2)用结构体变量作实参

(3)用指向结构体变量(或数组)的指针作实参,将结构体变量(或数组)的地址传给形参

例题:有一个结构体变量stu,内含学生学号、姓名和3门课程的成绩。通过调用函数print中将它们输出,要求:

先用结构体变量作函数参数: 

#include<stdio.h>
#include<string.h>
struct student
{
	int num;
	char *name;
	float score[3];
};
void print(struct student);
void main()
{
	struct studen
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值