结构体小结1(从本子上搬以前的笔记真是累死了)
来源:互联网 发布:ubuntu怎么退出vim 编辑:程序博客网 时间:2024/04/29 23:46
结构体
1.声明一个结构体
struct 自定义结构体类型名 结构体变量名
同时必须声明结构体内部的所有成员类型
例如声明一个学生的成绩单
struct Student Score
{
char aName[128];
int iAge;
int iNum;
int iScore;
};
这是一个结构体的声明,其中包括了姓名,年龄,学号,分数。
使用这种方法可以在主函数中声明自己需要的结构体
例如
struct Student Score;
当然可以声明结构体数组
例如
struct Student Score[3];
进一步的我们可以使用typedef关键字使得我们声明起来更方便
typedef struct student
{
char aName[128];
int iAge;
int iNum;
int iScore;
}STU;//建议这里大写,不知道为什么如果用小写总是容易出错//
这样声明后就可以方便的使用STU来声明了。
例如
STU Score[3];和用struct 声明是一样的。
结构体的大小:
结构体的大小是声明结构体内所有成员大小的总和
例如上边的结构体(X86_64系统)
sizeof(Score)=128+4+4+4=140字节
初始化及访问结构体内部的元素:
初始化使用"."运算符
例如初始化一个成绩: Score.iScore=95;
特别的对于这种名字为字符串的赋值时,建议使用字符串复制函数strcpy()函数来初始化。
strcpy(Score.iScore,"zhang san");
下面是一个完整的程序:
使用结构体记录三个学生的成绩,姓名,年龄,学号,返回成绩最大的成绩和学号。
#include<stdio.h>
#include<string.h>
#define SUCCESS 1
#define FAILL (-1)
#define std STD
typedef struct Student
{
int Scoure;
int iNumber;
char cName[128];
int iAge;
} std;/*使用typedef后自定义名称建议大写*/
int find_high_score(std *ptr,const unsigned int iLen,int *Max,int *MaxN);
int main()
{
std student[3];
int Max;
int MaxN;
int iRet;
memset(student,0,sizeof(student));
student[0].Scoure =80;
student[0].iNumber =23;
student[0].iAge =17;
strcpy(student[0].cName ,"zhang san");
student[1].Scoure =92;
student[1].iNumber =24;
student[1].iAge =17;
strcpy(student[0].cName ,"wang er");
student[2].Scoure =85;
student[2].iNumber =25;
student[2].iAge =17;
strcpy(student[0].cName ,"li si");
iRet = find_high_score(student,sizeof(student)/sizeof(std), &Max, &MaxN);
if(iRet!=SUCCESS)
{
return iRet;
}
printf("The hinghest score is %d\n",Max);
#include<string.h>
#define SUCCESS 1
#define FAILL (-1)
#define std STD
typedef struct Student
{
int Scoure;
int iNumber;
char cName[128];
int iAge;
} std;/*使用typedef后自定义名称建议大写*/
int find_high_score(std *ptr,const unsigned int iLen,int *Max,int *MaxN);
int main()
{
std student[3];
int Max;
int MaxN;
int iRet;
memset(student,0,sizeof(student));
student[0].Scoure =80;
student[0].iNumber =23;
student[0].iAge =17;
strcpy(student[0].cName ,"zhang san");
student[1].Scoure =92;
student[1].iNumber =24;
student[1].iAge =17;
strcpy(student[0].cName ,"wang er");
student[2].Scoure =85;
student[2].iNumber =25;
student[2].iAge =17;
strcpy(student[0].cName ,"li si");
iRet = find_high_score(student,sizeof(student)/sizeof(std), &Max, &MaxN);
if(iRet!=SUCCESS)
{
return iRet;
}
printf("The hinghest score is %d\n",Max);
printf("The student Number is %d\n",MaxN);
return SUCCESS;
}
int find_high_score(std *ptr,const unsigned int iLen,int *Max,int *MaxN)/*传递进参数后使用ptr*/
{
int iMaxs;
int iMaxn;
int i;
if(NULL== ptr || NULL==Max || NULL==MaxN)/*注意==与=的使用*/
{
return FAILL;
}
if(0==iLen)
{
return FAILL;
}
iMaxs=ptr->Scoure;
iMaxn=ptr->iNumber;
for(i=0;i<iLen;i++)
{
if(iMaxs<ptr->Scoure)
{
iMaxs=ptr->Scoure;
iMaxn=ptr->iNumber;
}
ptr++;
}
*Max=iMaxs;/*不能改变传递进来的指针的值,可以改变指针指向的值*/
*MaxN=iMaxn;
return SUCCESS;
}
return SUCCESS;
}
int find_high_score(std *ptr,const unsigned int iLen,int *Max,int *MaxN)/*传递进参数后使用ptr*/
{
int iMaxs;
int iMaxn;
int i;
if(NULL== ptr || NULL==Max || NULL==MaxN)/*注意==与=的使用*/
{
return FAILL;
}
if(0==iLen)
{
return FAILL;
}
iMaxs=ptr->Scoure;
iMaxn=ptr->iNumber;
for(i=0;i<iLen;i++)
{
if(iMaxs<ptr->Scoure)
{
iMaxs=ptr->Scoure;
iMaxn=ptr->iNumber;
}
ptr++;
}
*Max=iMaxs;/*不能改变传递进来的指针的值,可以改变指针指向的值*/
*MaxN=iMaxn;
return SUCCESS;
}
0 0
- 结构体小结1(从本子上搬以前的笔记真是累死了)
- 从本子文件名中提取本子的名字
- 既然来都来了,那就把以前写在其他博客上的文章都搬过来吧
- 再就是很早之前在本子上写的一个作业了。
- 真的要累死了
- 最近的工作任务真是多,怕被累死呀!
- 累死了
- 累死了
- scanf的使用,对于以前的认知我真是弱爆了
- 点读笔写字App(2)——初始化本子上已经记录的文字
- 本子终于回来了
- 循环《紧紧的储蓄计划》累死了
- 如何才能把我以前别的网站上的Blog搬过来呀?
- 买本子,给狠狠的宰了一刀
- 以前的笔记都是在单机上记录为的是查询方便 关于javaWeb已经记录了150多万字的笔记
- 几个Linux函数(很久的整理了,从自己的新浪博客搬来)
- RHEL5笔记(以前的)
- Charles for Mac 4.0.1 破解版 – Mac上优秀的HTTP监视器和抓包工具(现在抓包真是越来越难了)
- C++中的左值和右值
- Ruby学习记录——安装Ruby
- 二、RFID与物联网关系(第一次作业)
- IOS协议代理方法 与 IOS中视图和控制器的关系
- -fPIC问题的解决.
- 结构体小结1(从本子上搬以前的笔记真是累死了)
- Archlinux上grails环境的搭建
- ORACLE的sign函数和DECODE函数
- 岁月感怀 --读庄周梦蝶有感
- 状态模式state(对像行为)
- Servlet工作原理解析
- 二进制翻转
- Android全屏的时候floatmenu依然被任务栏的位置截掉一半setSystemUiVisibility()
- 工作总结第十六天