结构体与共同体

数据的基本类型包括:整形(int)、实型(float)、字符型(char)。也介绍了一种构造类型数据——数组.但是数组中的各元素要求属于同一种类型。有时候,仅仅用一种类型是不够的,需要将不同的数据组合成一个有机的整体,以便于引用。我们于是定义了结构体的概念。

1 suruct  student  //student称为结构体类型名
2 {
3 int num;
4 char name[20];
5 int age;
6 float score;
7 char addr[30]
8 };

 注意不要忘记最后的分号

 一.定义结构体类型变量的方法

 (1)先声明结构体类型,再定义变量名

 struct student   student1,student2;

    (结构体类型)     (变量1) (变量2)    

      如果程序的规模较大,往往将对结构体类型的声明集中放到一个头文件中(以.h为后缀名)。哪个源文件需要用到此类的结构体只要包含该头文件即可。

 (2)在声明结构体类型的时候定义变量

 struct student{
int ...
char ...
int ...
}student1,student2;

 (3)直接定义结构体类型变量

struct{
int ...
char ...
int ...
}student1,student2;

 (2)和(3)的区别仅在于(3)中没有出现结构体名。但是在实际的应用中,我们一般采用(2)并且加上typedef的声明。

二.对于结构体类型的几点说明

(1)类型与变量是两个不同的概念。只能对变量进行赋值,存取或运算,而不能对一个类型进行这些操作。编译时,只对变量进行空间的分配,而不会对类型进行空间的分配。

(2)对结构体中的成员,也可以单独使用

(3)成员也可以是一个结构体变量

(4)成员名和程序中的变量名是可以相同的,两者并不冲突。

三.结构体变量的引用

结构体变量名.成员名

四.结构体变量的初始化

和其他的类型变量类似,对结构体变量可以在定义时指定初始值

 struct{
int ...
char ... 
 int ... }student1={“100010”,"XiaoQiang","98.5"};

 五. 一个结构体变量中可以存放一组数据(如学生的姓名、学号等)。如果有10个学生的数据需要参加运算显然需要用到数组。这就是结构体数组。

struct student{
int ...
char ...
float ...
}student1;
struct student  student1[10];

 六. 结构体数组的初始化

struct student student1[4]={{},{},{},{}};

---------------------------------------------------------------------------

七.指向结构体类型数据的指针

一个结构体变量的指针就是指向该变量所占据的内存的起始地址。可以设一个指针变量,用来指向一个结构体变量,此时,该指针变量的值是结构体变量的起始地址。指针变量也可以用来指向结构体数组中的元素。

#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;

void main()
{
struct student
{
int num;
char name[100];
float score;
};


struct student stu;
struct student *P;
p=&stu; //此处说明结构体的变量名不代表地址


cout<<stu.num<<endl;
cout<<(*p).num<<endl;
cout<<p->num<<endl;

我们看到的三种调用方式,第一种为结构体变量的调用,第二种为指向结构体指针的调用,第三种是第二种的变形。

八. 指向结构体数组的指针

 1 #include<stdio.h>
2 #include<iostream>
3
4 using namespace std;
5
6 struct student{
7 int num;
8 char name[];
9 float score;
10 };
11
12 struct student stu[3]={{},{},{}}; //定义个一个结构体数组
13
14 void main()
15 {
16 struct student *p;
17 p=stu; //数组名代表的初始地址
18 for (p=stu;p<stu+3;p++)
19 {
20 cout<<p->num<<p->name>>p->score<<endl;
21 }

我们再分析一下该结构体数组在内存中的存储方式:

和二维数组的存储方式一致。stu[0]理解为第一行第一列即stu[0][0]元素的首地址。

九 用结构体变量和指向结构体变量的指针作为函数参数

将一个结构体变量的值传给另一个函数,由三种方式;

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

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

这两种都是属于“值传递”的形式,所以,如果结构体的内容如果比较多,则传递就比较庞大,复杂。

例:

#include "stdafx.h"
#include <iostream>
using namespace std;
struct student
{
int num;
char name[10];
float score;
}stu={210,"XiaoHong",98};


int _tmain(int argc, _TCHAR* argv[])
{
void output(struct student);//the declear of the function


output(stu); //the call of the function
return 0;
}
//the define of the function
void output(struct student value)
{
cout<<value.name<<" "<<value.num<<" "<<value.score<<" "<<endl;
}
 

 (3)用指向结构体变量(或数组)的指针做实参

 例:

#include "stdafx.h"
#include <iostream>

using namespace std;
struct student
{
int num;
char name[10];
float score;
}stu={210,"XiaoHong",98};


int _tmain(int argc, _TCHAR* argv[])
{
struct student *p;
void output(struct student *value);//the declear of the function
output(&stu); //the call of the function
return 0;
}
//the define of the function
void output(struct student *value)
{
cout<<value->name<<" "<<value->num<<" "<<value->score<<" "<<endl;
}

 (未完......)

原文地址:https://www.cnblogs.com/CBDoctor/p/2318174.html