对C语言结构体知识点的学习以及复习相关基础知识

(一)结构体数组

#include <iostream>
struct student{
int number;
char name[10];/*注意字符变量的定义,是定义成字符变量,还是字符串变量呢?char name; 与后面的初始化"tang"字符串不一致*/
int code;
}studen[3]={1,"tang",99,2,"tangzhong",89,3,"hua",88};
void main ()
{
for(int i=0;i<3;i++)
{
printf("number=%d ,name= %s ,code=%d",studen[i].number,studen[i].name,studen[i].code );
/*在程序没有错误的时候一定要考虑到输出格式上的错误,开始写成了
printf(%d number= %c name= 这里面有两个错误,一个是%d的位置写错了,造成输出 1 number=,tang name=的格式
另一个错误是%c是字符格式,并不是字符串格式。。请注意*/
printf("/n");
}
getchar();
}
/*这个程序重点学习了结构体数组的初始化,以及结构体变量的引用
学习笔记:
1、结构体定义只是一种数据结构,类似于类CLASS形式的一种数据结构,并不是存在的实体,只是一种自定义的类型
2、结构体变量是这种类型的变量,就像是int n中的n一样只是一个变量,它的值可以像变量一样改变
3、结构体数组是和一般数组类似,只是它的每个元素都具有结构体的结构。

4、得到的想法是:
进一步思考到用结构体去实现线性结构的链表,及其对链表的建立,结点的建健、查找、删除、插入等综合操作。。自己写出这个程序,那么我们的结构体知识点及数据结构的还有相关的基础知识也就撑握得差不多了。我认为是这样的。。所以近来加大了上机力度和时间。。

(二)指向结构体类型数据的指针

/*指向结构体类型数据的指针*/
#include<iostream>
main()
{
struct student
{
long num;
char name[20];
char sex;
float score;
};
struct student stu_1;//定义一个结构体变量stu_1;
struct student *p; //定义一个指向struct student 结构体类型的指针变量p
p=&stu_1;//结构体变量的地址赋给变量p,使指针P指向结构体变量stu_1;
stu_1.num=891001;
strcpy(stu_1.name,"lilin");//注意一个问题,字符串不能直接赋给变量,要用strcpy函数把字符串copy到字符数组里面去!
//上面的这个strcpy(stu_1.name,"lilin");不能写成stu_1.name="lilin"编译会出错
stu_1.sex='M';//字符变量(不是字符串数组)可以直接赋值,但是注意引号是单引号,不能写成这样stu_1.sex="M";
stu_1.score=89.5;
printf("No. :%ld/nname:%s/nsex:%c/nscore:%f/n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("No. :%ld/nname:%s/nsex:%c/nscore:%f/n",(*p).num,(*p).name,(*p).sex,(*p).score);
getchar();

}
/* 首先对指针进行复习:
1、指针变量的定义

(三)指向结构体数组的指针

(四)用结构体变量作函数的指针

/*结构体变量作函数参数*/
#include<iostream>
#define FORMAT "%d/n%s/n%f/n%f/n%f/n"//这里采用了宏不错的想法!方便,记得引用时也要大写。。
//而且后面的参数类型要对上哦!
struct student //结构体类型struct student 被定义成外部类型,同一源文件中的函数都可以用它
//定义结构体变量的类型
{
int num;
char name[20];
float score[3];
};
main()
{
void print(struct student); //声明print函数
struct student stu; //定义结构体student的变量stu;
//给结构体成员赋值
stu.num=12345;
strcpy(stu.name,"lili");
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.6;
print(stu);//调用print函数,参数传递是传递结构体变量stu
getchar();
}
void print(struct student stu)//由于结构体struct student被定义为外部类型,所以print函数也可把形参stu定义为struct student类型
{
printf(FORMAT,stu.num ,stu.name,stu.score[0],stu.score[1],stu.score[2]);
printf("/n");
}

(五)用指向结构体的指针作函数参数

/*用指向结构体变量类型的指针作函数的参数*/

#include<iostream>
#define FORMAT "%d/n%s/n%f/n%f/n%f/n"//这里采用了宏不错的想法!方便,记得引用时也要大写。。
//而且后面的参数类型要对上哦!
struct student //结构体类型struct student 被定义成外部类型,同一源文件中的函数都可以用它定义结构体变量的类型
{
int num;
char name[20];
float score[3];
}stu={1245,"lili",67.5,89,78.6};

main()
{
void print(struct student *p); //声明print函数(+++++ void print(stuct student *) 也没有错,也正确++)
print(&stu);//调用print函数,参数是地址,传给print函数的形参指向结构体变量类型的指针P
getchar();
}
void print(struct student *p)//由于结构体struct student被定义为外部类型,所以print函数也可把形参stu定义为struct student类型
{
printf(FORMAT,p->num ,p->name,p->score[0],p->score[1],p->score[2]);
printf("/n");
}

/**学习笔记
有三点值得注意的问题
1、struct student结构体被定义成外部类型,属全局类型,同一源文件中的所有函数都可以用来定义这种结构的变量
2、用指向结构体类型的指针当函的参数的时候,要注意在函数的声明或定义中,要说明形参的类型void print(struct student *p);或void print(struct student *);
3、注意这样的写法
scanf("%d%s%f%f%f",&stu.num,stu.name,&stu.score[0],&stu.score[1],&stu.score[2])
这里面的stu.name前面没有&符号,因为name是一个字符数组,它本身就是地址,所以不用加

用指针作函数的参数,能提高运行效率。。。。。。。。。。。。。。。。。。。。。。。。

(六)用结构体实现简单链表

#include <iostream>
#define NULL 0
struct student
{long num;
float score;
struct student *next;
};
main()
{
struct student stu_1,stu_2,stu_3;//定义了三个结构体struct student 变量
struct student *head,*p;
head=&stu_1;//让head指针指向链表的第一个结点;
stu_1.num=99901;stu_1.score=89.5;stu_1.next=&stu_2;
stu_2.num=99902;stu_2.score=87.6;stu_2.next=&stu_3;
stu_3.num=99903;stu_3.score=88.4;stu_3.next=NULL;
p=head;//P指向第一个结点
for(int i=0;i<3;i++)//用FOR循环,因为我已知道有多少个结点,null这个值没有用上。当不知道有多少结点的时候,要用循环while
{
printf("num=%ld,score=%f/n",p->num ,p->score );
p=p->next;//第一个结点数据输出后,让P指向第下一个结点
};
getchar();
}

/*这是一个静态链表,完全由自己从头书写,
学习笔记:
1、最后的链表输出是已知链表结点数,所有用了FOR循环
最好应该用while循环
do
{
printf("num=%ld,score=%f/n",p->num ,p->score );
p=p->next;//第一个结点数据输出后,让P指向第下一个结点
while(p!=NULL);
*/

学好了这些东西,才可以进行动态链表的程序设计。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值