C语言学习

 第一章:结构体

1、结构体形式:

   struct 标识符
    {
    类型1:成员1;
    类型2:成员2;
    ....
    }stu1,stu2;   //stu1、stu2表示定义变量

注:1)里面可以存放不同的类型
        2)由多个不同类型组成(自定义类型)


Eg:

struct Student
{
    char name[10];
    int id;
    float score[3];
}std3,std4;//std3,std4 全局变量
int main()
{
    int a = 1;
    //struct Student std1,std2;//struct Strudent 结构体类型 定义  创建并初始化
    struct Student std1 = {"xiaohei",1234,{70.0,80.0,90.0}};
    struct Student std2 = {"xiaobai",1432,{90.0,80.0,99.0}};

    //使用结构体成员(结构体变量.成员)
    printf("%s %d %f %f %f\n",std1.name,std1.id,std1.score[0],std1.score[1],std1.score[2]);
    //如何修改成员
    std1.id = 2222;
    std1.name[0] = 'm'; //m是字符,是要单引号
    //字符串就是数组类型,不能直接改,要通过循环一个一个改
    printf("%s %d %f %f %f\n",std1.name,std1.id,std1.score[0],std1.score[1],std1.score[2]);
    return 0;
}

struct Student
{
	char name[10];
	int id;
	float score[3];
}std3,std4;//std3,std4 全局变量
int main()
{
	int a = 1;
	//struct Student std1,std2;//struct Strudent 结构体类型 定义  创建并初始化
	struct Student std1 = {"xiaohei",1234,{70.0,80.0,90.0}};
	struct Student std2 = {"xiaobai",1432,{90.0,80.0,99.0}};

	//使用结构体成员(结构体变量.成员)
	printf("%s %d %f %f %f\n",std1.name,std1.id,std1.score[0],std1.score[1],std1.score[2]);
	//如何修改成员
	std1.id = 2222;
	std1.name[0] = 'm'; //m是字符,是要单引号
	//字符串就是数组类型,不能直接改,要通过循环一个一个改
	printf("%s %d %f %f %f\n",std1.name,std1.id,std1.score[0],std1.score[1],std1.score[2]);


	return 0;
}

2、结构体数组

    结构体数组定义

    struct   结构体名
    {
        成员列表
    }数组名[长度]


Eg:

struct Student
{
    char name[10];
    int id;
    float score[3];
}std3[3] = {{"xiaohei",123,{60.0,70.0,80.0}},{"xiaobai",432,{90.0,80.0,99.0}},{"xiaolu",444,{60.0,70.0,88.0}}};
//std3表示存放3个结构体   在这些表示全局的定义,在main里面也可以
int main()
{
    //输出结构体数组成员信息
    int i;
    for(i=0;i<3;i++){
        //使用结构体成员(结构体变量.成员)
        printf("%s %d %f %f %f\n",std3[i].name,std3[i].id,std3[i].score[0],std3[i].score[1],std3[i].score[2]);//注意:std3[3]表示的是数组,而不是结构体变量,点后面的才是
    
    }
    
    /*请将xiaobai的id改为999*/
    printf("将xiaobai的id改为999:\n");
    std3[1].id=999;
    printf("%s %d %f %f %f\n",std3[1].name,std3[1].id,std3[1].score[0],std3[1].score[1],std3[1].score[2]);

    return 0;
}

struct Student
{
	char name[10];
	int id;
	float score[3];
}std3[3] = {{"xiaohei",123,{60.0,70.0,80.0}},{"xiaobai",432,{90.0,80.0,99.0}},{"xiaolu",444,{60.0,70.0,88.0}}};
//std3表示存放3个结构体   在这些表示全局的定义,在main里面也可以
int main()
{
	//输出结构体数组成员信息
	int i;
	for(i=0;i<3;i++){
		//使用结构体成员(结构体变量.成员)
		printf("%s %d %f %f %f\n",std3[i].name,std3[i].id,std3[i].score[0],std3[i].score[1],std3[i].score[2]);//注意:std3[3]表示的是数组,而不是结构体变量,点后面的才是
	
	}
	
	/*请将xiaobai的id改为999*/
	printf("将xiaobai的id改为999:\n");
	std3[1].id=999;
	printf("%s %d %f %f %f\n",std3[1].name,std3[1].id,std3[1].score[0],std3[1].score[1],std3[1].score[2]);

	return 0;
}

3、结构体指针

结构:
    struct Book
    {
        char name[20];
        float money;
    
    }bk,*p,arrbk[3];
利用指针访问结构体中的成员
1)(…*指针变量名).成员名
2)指针变量名->成员名

//定义结构体
struct Student
{
	char name[10];
	int id;
	float score[3];
}std1,std3[3],*p;//全局变量

int main(){
	struct Student std1 = {"xiaohei",123,{60.0,70.0,80.0}};
	struct Student * p = &std1;
/*	std1.name;
	//1)(…*指针变量名).成员名
	(*p).name;
	//2)指针变量名->成员名
	p->id;
*/
	printf("方法一:\n");
	printf("%s %d %f %f %f\n",(*p).name,(*p).id,(*p).score[0],(*p).score[1],(*p).score[2]);
	printf("方法二:\n");
	printf("%s %d %f %f %f\n",p->name,p->id,p->score[0],p->score[1],p->score[2]);
	return 0;
}

结果:

4、函数传递结构体

    1)、成员值
    2)、传递结构体  void modify(struct Student a)
    3)、传递结构体地址   void modify(struct Student *p)

#include<stdio.h>
//定义结构体
struct Student
{
	char name[10];
	int id;
	float score[3];
}std1,std3[3],*p;//全局变量
struct Student* modify(struct Student *q)
{
	struct Student std5 = {"xiaohei",123,{60.0,70.0,80.0}};
	q->id = 999;
	(*q).id = 888;
	return &std5;  //返回值返回结构体
}


int main(){
	struct Student std1 = {"xiaohei",123,{60.0,70.0,80.0}};
	struct Student * p = &std1;
/*	std1.name;
	//1)(…*指针变量名).成员名
	(*p).name;
	//2)指针变量名->成员名
	p->id;
*/
	//1、成员值
	//2、传递结构体  void modify(struct Student a)
	//3、传递结构体地址   void modify(struct Student *p)
	modify(&std1);
	printf("%s %d %f %f %f\n",p->name,p->id,p->score[0],p->score[1],p->score[2]);
	return 0;
}

结果:修改了id的值

第二章:链表

1、链表的创建

1、

  • 18
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值