版权声明: https://blog.csdn.net/JerehEdu/article/details/50675707
在开发过程中,经常会需要处理一组不同类型的数据,比如学生的个人信息,由姓名、年龄、性别、身高等组成,因为这些数据是由不同数据类型组成的,因此不能用数组表示,对于不同数据类型的一组数据,可以采用结构体来进行存储。当然,对于面向对象的语言来说,最好是用类来表示,但是C语言是面向过程的,因此选择用结构体来表示。
一.结构体的定义 |
struct 结构体名{ 类型名 成员名1; 类型名 成员名2; ... ... 类型名 成员名n; };
二.结构体的变量声明 |
1.先定义结构体类型,再定义变量
代码
//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义学生信息的结构体
*/
struct student{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
};
int main(int argc, const char * argv[]) {
//声明结构变量
struct student student1;
struct student student2;
return 0;
}
三.定义结构体类型的同时定义变量 |
代码
//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义学生信息的结构体,并声明两个学生结构变量student1和student12
*/
struct student{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} student1, student2;
int main(int argc, const char * argv[]) {
return 0;
}
四. 直接定义结构体类型变量,省略类型名 |
代码
//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 直接声明两个结构体变量student1和student2
*/
struct{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} student1, student2;
int main(int argc, const char * argv[]) {
return 0;
}
五.结构体的嵌套 |
1结构体中可以包含,但是不允许对结构体本身递归使用。
代码
<pre name="code" class="objc">/
/
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义日期结构体
*/
struct date{
unsigned int year;
unsigned int month;
unsigned int day;
};
/**
* 定义学生结构体
*/
struct student{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
struct date birthday; //出生日期 (date结构体)
};
int main(int argc, const char * argv[]) {
return 0;
六.结构体的初始化 |
<一> 结构体变量可以在声明的时候一次性给多个成员初始化,但是需要注意的是初始化的顺序必须和定义结构体成员的顺序一样,初始化成员的个数是可以少于总成员个数。
<二> 声明结构变量后,可以采用结构变量名.成员名来为其赋值或取值。
<三> 声明结构变量后,可以整体接收相同类型的其他结构变量的值。
代码
/
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义日期结构体
*/
struct date{
unsigned int year;
unsigned int month;
unsigned int day;
};
/**
* 定义学生结构体
*/
struct student{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
struct date birthday; //出生日期
};
int main(int argc, const char * argv[]) {
//<一> 一次性给多个成员赋值
struct date birth1 = {1992, 1, 1};
struct student student1 ={"jredu", 21, 'f', 180.0, birth1};
//<二>对单个成员赋值
student1.age = 20;
student1.height = 178.0;
//<三>相同类型的变量间可进行整体赋值
struct student student2 = student1;
return 0;
}
七.结构体的使用 |
结构体是我们自定义的一种数据类型,但是实际上和系统提供给我们的基本数据类型的使用是一样的,因此,除了可以用结构做变量,还可以用结构体做数组、指针、函数。
1结构数组
用数组来存储一组结构体类型的变量,比如存放一组学生的结构数组。
在使用结构数组的时候和上面讲的结构变量一样,同样可以通过三种方式来得到结构数组。
代码
/**
* <一>先定义结构体
*/
struct student{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} ;
int main(int argc, const char * argv[]) {
//再声明结构数组
struct student stus[10];
return 0;
}
代码
/**
* <二>定义结构体同时直接声明结构数组
*/
struct student{
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} stus[10];
代码
/**
* <三>直接声明结构数组
*/
struct {
char name[100]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} stus[10];
2指向结构体的指针
要想使用指针来间接改变数据,必须用相同类型的指针去指向对象。结构体类型的变量或者数组在使用的时候就需要使用结构体类型的指针。
代码
//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义结构体
*/
struct student{
char *name; //姓名
unsigned int age; //年龄
} ;
int main(int argc, const char * argv[]) {
//声明结构变量
struct student student1 = {"jredu", 21};
//定义一个结构指针
struct student *ptr = &student1;
//访问结构成员,比如得到学生信息
//方式1:直接使用结构变量
printf("name = %s,age = %u\n",student1.name, student1.age);
//方式2:通过指针得到结构变量
printf("name = %s,age = %u\n", (*ptr).name, (*ptr).age);
//方式3:直接用指针
printf("name = %s,age = %u\n",ptr->name, ptr->age);
return 0;
}
3结构体做函数的参数
代码
//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义结构体
*/
struct student{
char *name; //姓名
unsigned int age; //年龄
} ;
void func1(struct student tempStu);
void func2(struct student *ptrStu);
int main(int argc, const char * argv[]) {
//声明结构变量
struct student student1 = {"jredu", 21};
struct student student2 = student1;
//调用参数为结构变量的函数
func1(student1);
printf("student1 name = %s\n",student1.name);
//调用参数为结构变量的函数
func2(&student2);
printf("student2 name = %s\n",student2.name);
return 0;
}
void func1(struct student tempStu){
tempStu.name = "apple";
}
void func2(struct student *ptrStu){
ptrStu->name = "apple";
}
八、结构体的简化 |
typedef可以对数据类型进行重命名,因此在定义结构体的时候可以使用它来简化操作。
代码
//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#include <stdio.h>
/**
* 定义结构体
*/
typedef struct {
char *name; //姓名
unsigned int age; //年龄
} Student;
int main(int argc, const char * argv[]) {
//声明结构变量
Student student1 = {"jredu", 21};
return 0;
}
作者:杰瑞教育
出处: http://www.cnblogs.com/jerehedu/
版权声明:本文版权归烟 台 杰瑞教育 科 技有限公司和CDSN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:
出处: http://www.cnblogs.com/jerehedu/
版权声明:本文版权归烟 台 杰瑞教育 科 技有限公司和CDSN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询: