在开发过程中,经常会需要处理一组不同类型的数据,比如学生的个人信息,由姓名、年龄、性别、身高等组成,因为这些数据是由不同数据类型组成的,因此不能用数组表示,对于不同数据类型的一组数据,可以采用结构体来进行存储。当然,对于面向对象的语言来说,最好是用类来表示,但是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[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
}; int main(int argc, const char * argv[]) { //声明结构变量
struct student student1;
struct student student2; return ;
}
三.定义结构体类型的同时定义变量

代码

//
// 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[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} student1, student2; int main(int argc, const char * argv[]) { return ;
}
四. 直接定义结构体类型变量,省略类型名

代码

//
// main.c
// 结构体
//
// Created by jerei on 15-12-27.
// Copyright (c) 2015年 jerehedu. All rights reserved.
// #include <stdio.h> /**
* 直接声明两个结构体变量student1和student2
*/
struct{
char name[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} student1, student2; int main(int argc, const char * argv[]) { return ;
}
 
五.结构体的嵌套

1结构体中可以包含,但是不允许对结构体本身递归使用。

代码

//
// 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[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
struct date birthday; //出生日期 (date结构体)
}; int main(int argc, const char * argv[]) {
return ;
六.结构体的初始化

<一> 结构体变量可以在声明的时候一次性给多个成员初始化,但是需要注意的是初始化的顺序必须和定义结构体成员的顺序一样,初始化成员的个数是可以少于总成员个数。

<二> 声明结构变量后,可以采用结构变量名.成员名来为其赋值或取值。

<三> 声明结构变量后,可以整体接收相同类型的其他结构变量的值。

代码

/
// 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[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
struct date birthday; //出生日期
}; int main(int argc, const char * argv[]) { //<一> 一次性给多个成员赋值
struct date birth1 = {, , };
struct student student1 ={"jredu", , 'f', 180.0, birth1}; //<二>对单个成员赋值
student1.age = ;
student1.height = 178.0; //<三>相同类型的变量间可进行整体赋值
struct student student2 = student1; return ;
}
七.结构体的使用

  结构体是我们自定义的一种数据类型,但是实际上和系统提供给我们的基本数据类型的使用是一样的,因此,除了可以用结构做变量,还可以用结构体做数组、指针、函数。

1结构数组

  用数组来存储一组结构体类型的变量,比如存放一组学生的结构数组。

  在使用结构数组的时候和上面讲的结构变量一样,同样可以通过三种方式来得到结构数组。

代码

/**
* <一>先定义结构体
*/
struct student{
char name[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} ; int main(int argc, const char * argv[]) { //再声明结构数组
struct student stus[]; return ;
}

代码

/**
* <二>定义结构体同时直接声明结构数组
*/
struct student{
char name[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} stus[];

代码

/**
* <三>直接声明结构数组
*/
struct {
char name[]; //姓名
unsigned int age; //年龄
char sex; //性别
double height; //身高
} stus[];

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", }; //定义一个结构指针
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 ;
}

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", };
struct student student2 = student1; //调用参数为结构变量的函数
func1(student1);
printf("student1 name = %s\n",student1.name); //调用参数为结构变量的函数
func2(&student2);
printf("student2 name = %s\n",student2.name); return ;
} 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", }; return ;
}
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

ios开发中的C语言学习—— 结构体简介的更多相关文章

  1. go语言学习-结构体

    结构体 go语言中的结构体,是一种复合类型,有一组属性构成,这些属性被称为字段.结构体也是值类型,可以使用new来创建. 定义: type name struct { field1 type1 fie ...

  2. [iOS]怎样在iOS开发中切换显示语言实现国际化

    1.在Project设置,加入中英两种语言: 2.新建Localizable.strings文件,作为多语言相应的词典,存储多种语言,点击右側Localization,勾选中英: watermark/ ...

  3. iOS开发中UIDatePicker控件的使用方法简介

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四 ...

  4. iOS开发中调试小技巧

    对于软件开发而言,调试是必须学会的技能,重要性不言而喻.对于调试的技能,基本上是可以迁移的,也就是说你以前在其他平台上掌握的很多调试技巧,很多也是可以用在iOS开发中.不同语言.不同IDE.不同平台的 ...

  5. iOS - 开发中调试小技巧

    对于软件开发而言,调试是必须学会的技能,重要性不言而喻.对于调试的技能,基本上是可以迁移的,也就是说你以前在其他平台上掌握的很多调试技巧,很多也是可以用在iOS开发中.不同语言.不同IDE.不同平台的 ...

  6. 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...

  7. ios学习笔记之block在ios开发中的应用

    一.什么是Blocks      Block是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,但是其运行需要编译器和运行时支持,从ios4.0开始就很好的支持Block. 二. ...

  8. 浅谈iOS开发中多语言的字符串排序

    一.前言 在iOS开发中,一个经常的场景是利用tableview展示一组数据,以很多首歌曲为例子.为了便于查找,一般会把这些歌曲按照一定的顺序排列,还会加上索引条以便于快速定位. 由于歌曲名可能有数字 ...

  9. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

随机推荐

  1. Dell R710、720等系列类服务器 U盘安装centos6.5 操作系统

    一般全新服务器创建系统的时候,没做raid 会报错,如下: 解决: 开机启动时按F10,进入下面界面. 在LC设置-语言和键盘设置选项里可以选择界面显示的语言 在界面主页选项里选择"配置RA ...

  2. (转)字符编码笔记:ASCII,Unicode和UTF-8

    字符编码笔记:ASCII,Unicode和UTF-8 访问地址:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html

  3. IntelliJIDEA 14创建Maven管理的Java Web项目

    1.新建项目,选择Maven,点击Next继续. 接着输入项目名 接着直接点击Finish即可 下图就是创建完毕后的Maven项目,双击pom.xml查看POM文件内容,可以自行添加Maven的依赖. ...

  4. matlab微分方程dsolve使用

    y=dsolve('Dy=exp(-x-y-2)','y(0)=-2','x') dy/dx 写成Dy (注意大小写) y(0)=-2 表示初始条件 'x'表示积分变量

  5. JavaScript-hash数组for in 函数

    什么是数组:内存中,连续存储多个数据的存储空间,再起一个名字为什么; 为什么:现实存储多个相关数据,都是集中存储,共同一个名字 程序=数据结构+算法 好的数据结构可以极大的提高程序的执行效率 何时使用 ...

  6. Java 修改Windows注册表,以实现开机自启动应用程序。

    使用Java修改Windows注册表,使用最基本的就是cmd命令. 事例和运行结果如下所示: package day01; import java.io.IOException; /* 1,reg a ...

  7. Common Scenarios to avoid with DataWarehousing

    Database Design Rule Description Value Source Problem Description 1 Excessive sorting and RID lookup ...

  8. C++ 用于大型程序的工具

    <C++ Primer 4th>读书笔记 相对于小的程序员团队所能开发的系统需求而言,大规模编程对程序设计语言的要求更高.大规模应用程序往往具有下列特殊要求: 1. 更严格的正常运转时间以 ...

  9. macbook 快捷键

    macbook  air快捷键应用 Command + 空格键  = 切换输入法Command + Control + F = 全屏(Command + Shift + F Chrome全屏 Comm ...

  10. [Python爬虫] 在Windows下安装PIP+Phantomjs+Selenium

    最近准备深入学习Python相关的爬虫知识了,如果说在使用Python爬取相对正规的网页使用"urllib2 + BeautifulSoup + 正则表达式"就能搞定的话:那么动态 ...