structure

  不管是什么东东,理论是一部分,我个人觉得更重要的一环还是在应用上,所以在这里叙述性的东西比较少,我挑了几个关于结构体重点的部分来学习结构体,对其进行一一举例,我想通过例子,理解起来将会更容易,更准确,更深刻。

1,有关结构体类型

结构体定义的一般形式

struct 结构体名

{

类型数据 成员名1

类型数据 成员名2

….... 成员名...;

类型数据 成员名n;

};//最后一个分号标志着类型定义的结束

说明:(1)结构体内的成员名不能重名,但可以与结构体之外的同名

2)结构体类型占用的存储空间的字节数等于所有成员占用存储空间字节数的总和

 

结构体所占用的字节数
#include"iostream"
using namespace std;
struct date
{
int year ;
int month;
int day;
};
struct list
{
int num;
char name[10];
char sex;
int age;
char add[20];
float score;
};
int main()
{
cout
<<sizeof(struct date)<<endl<<sizeof(struct list)<<endl;
}

 

 

 

2,结构体变量的引用

引用的一般形式 结构体变量名.成员名

(“.”结构体成员运算符,用来引用结构体变量的成员)

说明:(1)“结构体变量名.成员名”是一个整体,表示一个成员变量

2)若成员变量本身又是一个结构体变量,只能引用最低一级的成员变量

 

 

//结构体变量的引用(保存你的个人信息)
#include"iostream"
using namespace std;
struct list
{
char num[100];
int age;
char ID[20];
char name[10];
char sex;
char add[20];
float score;
};
int main()
{
struct list s;
scanf(
"%s%d%s%s%s%s%f",&s.num,&s.age,&s.ID,&s.name,&s.sex,&s.add,&s.score);
cout
<<s.num<<endl;
cout
<<s.age<<endl;
cout
<<s.ID<<endl;
cout
<<s.name<<endl;
cout
<<s.sex<<endl;
cout
<<s.add<<endl;
printf(
"%.2lf\n",s.score);
}

 

 

 

3.结构体数组的引用

一般形式: 数组名[下标]。成员名

例如:

 

对候选人得票的统计,输入每一张选票的名字,最后输出各候选人得票结果。
#include"iostream"
#include
"string.h"
using namespace std;
struct person leader[100];
struct person
{
char name[10];
int count;
}leader[
3]={{"li",0},{"zhang",0},{"fang",0}};
int main()
{
int i;
char leader_name[10];
scanf(
"%s",&leader_name);//循环初始化
while(strcmp(leader_name,"end")!='\n')//处理是否有效票
{
for(i=0;i<3;i++)
if(strcmp(leader_name,leader[i].name)==0)
leader[i].count
++;
scanf(
"%s",&leader_name);
}
for(i=0;i<3;i++)
cout
<<leader[i].name<<endl<<leader[i].count<<endl;
}

 

 

 

4,通过指针变量引用结构体变量

一般形式: *指针变量名).成员名 指针变量名-->成员名

说明:(1)通过指向结构体变量的指针变量,引用结构体变量的成员,就是用“*指针变量名”代替“结构体变量名”。

 2)“->”表示指向结构体成员运算符。

如下,

 

代码
#include"iostream"
using namespace std;
int main()
{
struct list
{
int num;
char name[100];
float score;
}s
={1,"王永飞",100};
struct list *p=&s; //指针变量p指向结构体变量s
cout<<s.num<<" "<<s.name<<" "<<s.score<<endl;
cout
<<(*p).num<<" "<<(*p).name<<" "<<(*p).score<<endl;
cout
<<p->num<<" "<<p->name<<" "<<p->score<<endl;
}

 

 

 

5,结构体指针变量作为函数参数

用结构体类型的指针变量作为函数参数,就是把一个结构体变量的地址传递给函数。

举例如下,

 

代码
#include"iostream"
using namespace std;
struct book
{
int count ;
double price;
}b
={5,17.3};
void total(struct book *p)
{
double x;
x
=p->count*p->price;//数量*单价
printf("%.2f\n",x);
}
int main()
{
//struct book b={5,17.3};
total(&b);
}

 

 

 

 

代码
#include"iostream"
using namespace std;
#define N 4
struct list
{
int num;
char name[10];
float score;
};
void fun(struct list *p,int n)
{
struct list *q;
int i,max;
max
=p->score;
q
=p;
for(p++,i=1;i<n;i++)
{
if(p->score>max)
{
max
=p->score;q=p;
}
p
++;
}
cout
<<q->num<<" "<<q->name<<" "<<q->score<<endl;
}
int main()
{
struct list stu[N];
int i;
for(i=0;i<N;i++)
scanf(
"%d%s%f",&stu[i].num,&stu[i].name,&stu[i].score);
fun(stu,N);
}

 

 

 

6,位段结构体

目的:节省存储空间。

定义的一般形式

Struct 结构体名

{

 数据类型 位段名 1:长度;

数据类型 位段名 2:长度;

............ …;

数据类型 位段n: 长度;

 

}

说明:位段的类型只有 unsigned int int .

举例如下

 

struct
{
unsigned a:
1;
unsigned b:
1;
unsigned c:
1;
}data;

 

 

 

 

 

 注释:以上内容不够全面,希望读者朋友能提出宝贵意见和建议,以便我在以后的学习过程当中逐渐充实有关结构体的内容。谢谢!

posted @ 2010-10-07 10:57  聊聊IT那些事  阅读(602)  评论(0编辑  收藏  举报