ios开发中的C语言学习—— 结构体简介

  在开发过程当中,常常会须要处理一组不一样类型的数据,好比学生的我的信息,由姓名、年龄、性别、身高等组成,由于这些数据是由不一样数据类型组成的,所以不能用数组表示,对于不一样数据类型的一组数据,能够采用结构体来进行存储。固然,对于面向对象的语言来讲,最好是用类来表示,可是C语言是面向过程的,所以选择用结构体来表示。web

一.结构体的定义
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;
}

 

三.定义结构体类型的同时定义变量

代码app

//
//  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结构体中能够包含,可是不容许对结构体自己递归使用。

代码spa

//
//  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;

 

六.结构体的初始化

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

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

<三> 声明结构变量后,能够总体接收相同类型的其余结构变量的值。orm

代码对象

/
//  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;
}

 

七.结构体的使用

  结构体是咱们自定义的一种数据类型,可是实际上和系统提供给咱们的基本数据类型的使用是同样的,所以,除了能够用结构作变量,还能够用结构体作数组、指针、函数。blog

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";
}

 

8、结构体的简化

 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/ 
版权声明:本文版权归 杰瑞教育 技有限公司和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然保留追究法律责任的权利。
技术咨询:JRedu技术交流
相关文章
相关标签/搜索