C语言教程:结构体(用户定义的数据类型)

2018-10-0819:03:21编程语言入门到精通Comments6,107 views1字数 2166阅读模式

C语言中的结构体是一种用户定义的数据类型,可以保存不同类型的数据元素。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

结构体的每个元素都称为成员。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

它像C++中的模板和Java中的类一样工作。可以有不同类型的元素。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

例如,结构体可广泛用于存储学生信息,员工信息,产品信息,图书信息等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

定义结构体

struct关键字用于定义结构体。下面我们来看看在C语言中定义结构体的语法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

struct structure_name   
{  
    data_type member1;  
    data_type member2;  
    .  
    .  
    data_type memeberN;  
};
C

下面来看看如何在C语言中定义员工结构体的例子。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

struct employee
{   int id;
    char name[50];
    float salary;
};
C

这里,struct是关键字,employee是结构体的标签名称; idnamesalary是结构体的成员或者字段。让我们通过下面给出的图来理解它:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

C语言教程:结构体(用户定义的数据类型)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

声明结构体变量

我们可以为结构体声明变量,以便访问结构体的成员。声明结构体变量有两种方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

  1. 通过main()函数中的struct关键字
  2. 通过在定义结构时声明变量。

第一种方式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

下面来看看一下struct struct来声明结构变量的例子。它应该在主函数中声明。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

struct employee  
{   int id;  
    char name[50];  
    float salary;  
};
C

现在在main()函数中写入给定的代码,如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

struct employee e1, e2;
C

第二种方式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

下面来看看在定义结构体时声明变量的另一种方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

struct employee  
{   int id;  
    char name[50];  
    float salary;  
}e1,e2;
C

哪种方法好?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

但如果变量个数不固定,使用第一种方法。它为您提供了多次声明结构体变量的灵活性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

如果变量变量个数是固定的,使用第二种方法。它在main()函数代码中保存声明的变量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

访问结构成员

访问结构成员有两种方式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

  • 通过符号. (成员或点运算符)
  • 通过符号 ->(结构指针运算符)

下面下面来看看看代码访问p1变量的id成员的.操作符。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

p1.id
C

结构体示例

我们来看看一个简单的C语言结构示例。创建一个工程:structure,并在此工程下创建一个源文件:structure-example.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

#include <stdio.h>  
#include <string.h>  
struct employee
{
    int id;
    char name[50];
}e1;  //declaring e1 variable for structure  

int main()
{
    //store first employee information  
    e1.id = 1010;
    strcpy(e1.name, "Max Su");//copying string into char array  
    //printing first employee information  
    printf("employee 1 id : %d\n", e1.id);
    printf("employee 1 name : %s\n", e1.name);
    return 0;
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

employee 1 id : 1010
employee 1 name : Max Su
Shell

下面我们来看看如何使用C语言结构体来存储多个员工信息的示例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

创建一个源文件:structure-more-employee.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

#include <stdio.h>  
#include <string.h>  
struct employee
{
    int id;
    char name[50];
    float salary;
}e1, e2;  //declaring e1 and e2 variables for structure  

int main()
{
    //store first employee information  
    e1.id = 1001;
    strcpy(e1.name, "Max Su");//copying string into char array  
    e1.salary = 18000;

    //store second employee information  
    e2.id = 1002;
    strcpy(e2.name, "Julian Lee");
    e2.salary = 15600;

    //printing first employee information  
    printf("employee 1 id : %d\n", e1.id);
    printf("employee 1 name : %s\n", e1.name);
    printf("employee 1 salary : %f\n", e1.salary);

    //printing second employee information  
    printf("employee 2 id : %d\n", e2.id);
    printf("employee 2 name : %s\n", e2.name);
    printf("employee 2 salary : %f\n", e2.salary);

    return 0;
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html

employee 1 id : 1001
employee 1 name : Max Su
employee 1 salary : 18000.000000
employee 2 id : 1002
employee 2 name : Julian Lee
employee 2 salary : 15600.000000
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6458.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6458.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定