C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言结构

为什么使用结构?

在C中,有时需要存储一个实体的多个属性。实体不必仅具有一种类型的所有信息。它可以具有不同数据类型的不同属性。例如,实体学生可以具有其名称(字符串),卷编号(整数),标记(浮点数)。为了存储有关实体学生的此类信息,我们采用以下方法:
构造单个数组以存储名称,卷号和标记。 使用特殊的数据结构来存储不同数据类型的集合。
让我们详细了解第一种方法。
#include<stdio.h>
void main ()
{
  char names[2][10],dummy; // 2-dimensioanal character array names is used to store the names of the students 
  int roll_numbers[2],i;
  float marks[2];
  for (i=0;i<3;i++)
  {
    
    printf("Enter the name, roll number, and marks of the student %d",i+1);
    scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);
    scanf("%c",&dummy); // enter will be stored into dummy character at each iteration
  }
  printf("Printing the Student details ...\n");
  for (i=0;i<3;i++)
  {
    printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);
  }
}
输出
Enter the name, roll number, and marks of the student 1Arun 90 91        
Enter the name, roll number, and marks of the student 2Varun 91 56      
Enter the name, roll number, and marks of the student 3Sham 89 69
Printing the Student details...
Arun 90 91.000000                                                                      
Varun 91 56.000000  
Sham 89 69.000000
以上程序可以满足我们存储实体学生信息的要求。但是,该程序非常复杂,并且复杂度随着输入量的增加而增加。每个阵列的元素都连续存储,但是所有阵列可能不会连续存储在内存中。 C为您提供了另一种更简单的方法,您可以在其中使用特殊的数据结构,即可以在其中将与实体有关的不同数据类型的所有信息组合在一起的结构。

什么是结构

C中的结构是用户定义的数据类型,使我们能够存储不同数据类型的集合。结构的每个元素称为成员。模拟类和模板的使用,因为它可以存储各种信息
struct 关键字用于定义结构。让我们看看在c中定义结构的语法。
struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeberN;
};
让我们看一下为c中的实体员工定义结构的示例。
struct employee
{   int id;
    char name[20];
    float salary;
};
下图显示了上面示例中定义的结构员工的内存分配。
c结构内存分配
此处,结构是关键字; employee是该结构的名称; id ,name和salary是结构的成员或字段。让我们通过下面的图表来了解它:
c structure

声明结构变量

我们可以为结构声明一个变量,以便我们可以轻松地访问结构的成员。声明结构变量有两种方法:
通过main()函数中的struct关键字 通过在定义结构时声明变量。
第一种方法:
让我们看一下通过struct关键字声明结构变量的示例。应该在主函数中声明它。
struct employee
{   int id;
    char name[50];
    float salary;
};
现在在main()函数中编写给定的代码。
struct employee e1, e2;
变量e1和e2可用于访问存储在结构中的值。在这里,可以将e1和e2与 C++ 和Java 。
第二种方式:
让我们看看在以下位置声明变量的另一种方式: 定义结构的时间。
struct employee
{   int id;
    char name[50];
    float salary;
}e1,e2;

哪种方法很好

如果变量的数量不确定,则使用第一种方法。
如果没有,它使您可以灵活地多次声明结构变量。变量是固定的,请使用第二种方法。它将保存您的代码以在main()函数中声明变量。

访问结构成员

有两种访问结构成员的方法:
通过。(成员或点运算符) 通过->(结构指针运算符)
让我们看一下访问 p1 变量的 id 成员的代码。(成员)运算符。
p1.id

C结构示例

我们来看一个简单的C语言结构示例。
#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=101;  
   strcpy(e1.name, "Sonoo Jaiswal");//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;
}  
输出:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
让我们看一下 C语言中存储大量员工信息的结构示例。
#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=101;  
   strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array  
   e1.salary=56000;  
  
  //store second employee information  
   e2.id=102;  
   strcpy(e2.name, "James Bond");  
   e2.salary=126000;  
   
   //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;  
}  
输出:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4