📜  C中的结构体

📅  最后修改于: 2020-10-22 05:48:44             🧑  作者: Mango

C 结构体

为什么要使用结构体?

在C语言中,有些情况下我们需要存储一个实体的多个属性。实体不必仅具有一种类型的所有信息。它可以具有不同数据类型的不同属性。例如,一个实体Student可能具有其名称(字符串),卷编号(int),标记(float)。为了存储有关实体学生的此类信息,我们采用以下方法:

  • 构造用于存储名称,卷编号和标记的单个数组。
  • 使用特殊的数据结构来存储不同数据类型的集合。

让我们详细了解第一种方法。

#include
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中的结构体是用户定义的数据类型,使我们能够存储不同数据类型的集合。结构的每个元素称为一个成员。结构ca;模拟类和模板的使用,因为它可以存储各种信息

.struct关键字用于定义结构。让我们看看在c中定义结构的语法。

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

让我们看一下在c中为实体员工定义结构体的示例。

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

下图显示了以上示例中定义的结构体employee的内存分配。

在这里,struct是关键字;员工是结构的名称; id,名称和薪水是结构的成员或字段。让我们通过下面的图表来理解它:

声明结构体变量

我们可以为该结构体声明一个变量,以便我们可以轻松地访问该结构的成员。有两种方法来声明结构变量:

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

第一种方式:

让我们看一下通过struct关键字声明结构变量的示例。应该在main函数声明它。

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
#include   
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
#include   
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