您的位置:首页 > 编程语言 > C语言/C++

C语言(六)结构体

2015-02-06 09:40 155 查看

一、结构体概述

1.简介

结构体是一种数据类型,存储的元素可以是不同的数据类型。

结构体的组成元素,一般称为结构体成员。

2.定义

struct 结构体名{

类型名1 成员名1;

类型名2 成员名2;

……

类型名n 成员名n;   

};
3.定义结构体类型

struct Student {
char *name; // 姓名
int age; // 年龄
float height; // 身高
};
4.定义结构体变量

1)先定义结构体类型,再定义变量

2)在定义结构体类型的同时定义变量

3)直接定义结构体类型变量,省略结构体名

struct Student {
char *name;
int age;
};
struct Student stu;
或
struct Student {
char *name;
int age;
} stu;
或
struct {
char *name;
int age;
} stu;


5.注意点

1)不允许对结构体本身递归定义

下面的写法是错误的。

struct Student {
int age;
struct Student stu;
};


2)结构体内可以包含其他的结构体

struct Date {
int year;
int month;
int day;
};
struct Student {
char *name;
struct Date birthday;
};


3)定义结构体类型,只是说明了结构体类型的组成情况,并没有给它分配存储空间,

就像系统不会为int本身分配存储空间一样。只有定义了结构体类型的变量,才会分配存储空间。

struct Student {
char *name;
int age;
};
struct Student stu;
只有执行到定义结构体变量的那一行,系统才会为该变量分配存储空间。

4)结构体类型变量所占的存储空间是其成员所占内存之和,而且各个成员在内存中按照定义的顺序依次排序。

struct Student {
char *name; // 姓名
int age; // 年龄
float height; // 身高
};
在16位编译器环境下,一个Student变量所占用内存:2+2+4=8字节。

6.结构体的初始化

将各成员的初值,按顺序地放在“{}”中,并用逗号隔开,并一一赋值。

struct Student {
char *name;
int age;
};
struct Student stu = {"MJ", 27};<span style="font-family: Consolas; background-color: rgb(255, 255, 255);">	</span>


只能在定义变量的同时进行初始化赋值,初始化赋值和变量的定义不能分开。

struct Student stu;

stu={"MJ",26};是错误的。

二、结构体的使用

1.结构体

1)一般对结构体变量的操作是以成员为单位进行的,引用的一般形式为:结构体变量名.成员名

#include <stdio.h>
struct Student{
char *name;
int age;
};
int main(){
struct Student stu={"zhangsan",29};

struct Student stu2;
stu2.name="lisi";
stu2.age=30;

printf("%s\n",stu2.name);
printf("%d\n",stu2.age);
return 0;
}
2)如果某个成员也是结构体变量,可以连续和私用成员运算符,访问低一级的成员.
#include <stdio.h>
struct Date { int year; int month; int day; }; struct Student { char *name; struct Date birthday; };
int main(){
struct Student stu;

stu.birthday.year = 1986;
stu.birthday.month = 9;
stu.birthday.day = 10;

return 0;
}


3)相同类型的结构体变量之间可以进行整体赋值
struct Student{...};

struct Student stu1 ={...};

struct Student stu2 = stu1;

2.结构体数组

1)结构体数组有三种定义方式

struct Student {
char *name;
int age;
};
struct Student stu[5]; //定义1
struct Student {
char *name;
int age;
} stu[5]; //定义2
struct {
char *name;
int age;
} stu[5]; //定义3
2)结构体数组的初始化

struct {
char *name;
int age;
} stu[2] = { {"MJ", 27}, {"JJ", 30} };


3.结构体作为函数参数

将结构体变量作为函数参数进行传递时,其实传递的是全部成员的值。也就是将实参中的成员的值一一赋值给对应的形参成员。因此,形参的改变并不会影响到实参。

#include <stdio.h>
struct Student{
char *name;
int age;
};

void test(struct Student stu){
printf("%d\n",stu.age);
printf("%s\n",stu.name);

stu.name="xiaosi";	//修改属性
stu.age=30;
printf("%d\n",stu.age);
printf("%s\n",stu.name);

}
int main(){
struct Student stu1[2]={{"zhangsan",20},{"lisi",25}};//使用结构体数组
test(stu1[0]);//传递的参数为结构体数组的第一个元素
}


4.指向结构体的指针

1)每个结构体变量都有自己的存储空间和地址,因此指针也可以指向结构体变量。
2)结构体指针变量的定义形式,struct 结构体名称 *指针变量名
3)有了指向结构体的指针,那么就有三种访问结构体成员的方式
结构体变量名.成员名

{*指针变量名}.成员名

指针变量名->成员名
#include <stdio.h>
//定义一个结构体类型
int main(){
struct Student{
char *name;
int age;
};

//定义一个结构体变量
struct Student stu1={"zhangsan",21};
//定义一个指向结构体的指针变量
struct Student *p;
//初始化指向结构体的指针变量
p=&stu1;

printf("name=%s,age=%d\n",(*p).name,(*p).age);
printf("name=%s,age=%d\n",p->name,p->age);
printf("name=%s,age=%d\n",stu1.name,stu1.age);

return 0;
}
3.相同类型的结构体变量之间可以进行整体赋值

struct Student{...};

struct Student stu1 ={...};

struct Student stu2 = stu1;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: