结构

原文:https://www.studytonight.com/c/structures-in-c.php

结构是 C 语言中用户定义的数据类型,它允许我们将不同类型的数据组合在一起。结构有助于构造更有意义的复杂数据类型。它有点类似于数组,但是数组只保存相似类型的数据。但是另一方面,结构可以存储任何类型的数据,这更实用更有用。

比如:如果我要写一个程序来存储 Student 信息,里面会有 Student 的名字、年龄、分支、永久地址、父亲的名字等等,里面包含了字符串值、整数值等等,这个问题怎么用数组呢,我会要求一些可以把不同类型的数据放在一起的东西。

在结构上,数据以记录的形式存储。


定义结构

struct关键字用于定义结构。struct定义一个新的数据类型,它是主数据类型和派生数据类型的集合

语法:

struct [structure_tag]
{
    //member variable 1
    //member variable 2
    //member variable 3
    ...
}[structure_variables];

正如你在上面的语法中看到的,我们从struct关键字开始,然后它可以选择为你的结构提供一个名称,我们建议你给它一个名称,然后在花括号里面,我们必须提到所有的成员变量,这些变量只不过是不同类型的普通 C 语言变量,比如intfloatarray等。

在结束大括号之后,我们可以指定一个或多个结构变量,这也是可选的。

注意:结构类型声明中的右花括号后面必须跟一个分号(;)。

结构示例

struct Student
{
    char name[25];
    int age;
    char branch[10];
    // F for female and M for male
    char gender;
};

这里struct Student声明了一个保存学生详细信息的结构,该结构由 4 个数据字段组成,即nameagebranchgender。这些字段被称为结构元素或成员

每个成员可以有不同的数据类型,比如在这个例子中,name是一个char类型的数组,ageint类型的数组等等。学生是结构的名称,被称为结构标签


声明结构变量

可以声明结构的变量,可以与结构定义一起声明,也可以在结构定义之后声明。结构 变量的声明类似于任何其他数据类型的任何普通变量的声明。结构变量可以通过以下两种方式声明:


1)分别声明结构变量

struct Student
{
    char name[25];
    int age;
    char branch[10];
    //F for female and M for male
    char gender;
};

struct Student S1, S2;      //declaring variables of struct Student

2)用结构定义声明结构变量

struct **Student**
{
    char name[25];
    int age;
    char branch[10];
    //F for female and M for male
    char gender;
}S1, S2;

这里S1S2是结构的变量Student。然而,这种方法不太推荐。


访问结构成员

可以通过多种方式访问结构成员并为其赋值。没有结构,结构成员就没有单独的意义。为了给任何结构成员赋值,必须使用点.运算符(也称为句点成员访问运算符)将成员名称与结构变量链接起来。

例如:

#include<stdio.h>
#include<string.h>

struct Student
{
    char name[25];
    int age;
    char branch[10];
    //F for female and M for male
    char gender;
};

int main()
{
    struct Student s1;

    /*
        s1 is a variable of Student type and 
        age is a member of Student
    */
    s1.age = 18;
    /*
        using string function to add name
    */
    strcpy(s1.name, "Viraaj");
    /*
        displaying the stored values
    */
    printf("Name of Student 1: %s\n", s1.name);
    printf("Age of Student 1: %d\n", s1.age);

    return 0;
}

学生 1 的姓名:维拉杰学生 1 的年龄:18 岁

我们也可以使用scanf()通过终端给结构成员赋值。

scanf(" %s ", s1.name);
scanf(" %d ", &s1.age);

结构初始化

像任何其他数据类型的变量一样,结构变量也可以在编译时初始化。

struct **Patient**
{
    float height;
    int weight;  
    int age; 
};

struct Patient p1 = { 180.75 , 73, 23 };    //initialization

或者,

struct Patient p1;
p1.height = 180.75;     //initialization of each member separately
p1.weight = 73;
p1.age = 23;

结构数组

我们也可以声明一个结构变量的数组。其中数组的每个元素将代表一个结构变量。示例: struct employee emp[5];

下面的程序定义了一个大小为 5 的数组emp。数组emp的每个元素都属于Employee类型。

#include<stdio.h>

struct Employee
{
    char ename[10];
    int sal;
};

struct Employee emp[5];
int i, j;
void ask()
{
    for(i = 0; i < 3; i++)
    {
        printf("\nEnter %dst Employee record:\n", i+1);
        printf("\nEmployee name:\t");
        scanf("%s", emp[i].ename);
        printf("\nEnter Salary:\t");
        scanf("%d", &emp[i].sal);
    }
    printf("\nDisplaying Employee record:\n");
    for(i = 0; i < 3; i++)
    {
        printf("\nEmployee name is %s", emp[i].ename);
        printf("\nSlary is %d", emp[i].sal);
    }
}
void main()
{
    ask();
}

嵌套结构

C 语言也允许嵌套结构。嵌套结构是指一个结构有另一个结构作为成员变量

示例:

struct Student
{
    char[30] name;
    int age;
    /* here Address is a structure */
    struct Address
    {
        char[50] locality;
        char[50] city;
        int pincode;        
    }addr;
};

作为函数参数的结构

我们可以将结构作为函数参数传递,就像我们将任何其他变量或数组作为函数参数传递一样。

示例:

#include<stdio.h>

struct Student
{
    char name[10];
    int roll;
};

void show(struct Student st);

void main()
{
    struct Student std;
    printf("\nEnter Student record:\n");
    printf("\nStudent name:\t");
    scanf("%s", std.name);
    printf("\nEnter Student rollno.:\t");
    scanf("%d", &std.roll);
    show(std);
}

void show(struct Student st)
{
    printf("\nstudent name is %s", st.name);
    printf("\nroll is %d", st.roll);
}