C 语言中的结构体和函数

C 语言允许我们将结构体作为函数参数传递。在阅读本文之前,请先参考关于函数的文章。这将帮助您理解函数的概念。我们可以通过 3 种方式将结构体传递给函数:

  1. 将结构体的每个成员作为函数参数传递。这类似于将普通值作为参数传递。虽然这种方法易于实现,但我们不使用它,因为如果结构体的大小稍大,我们的工作就会变得非常麻烦。
  2. 将整个结构体作为值传递。
  3. 我们也可以传递结构体的地址(通过引用传递)。

C 语言中结构体和函数的示例

以下示例将向您解释如何通过值和通过引用将结构体传递给函数。

在 C 语言中按值传递结构体给函数

如果结构体是按值传递给函数的,那么在函数内部对结构体变量成员所做的更改不会影响原始结构体成员。

在这个关于 C 语言中结构体和函数的程序中,用户被要求输入学生姓名、第一年分数和第二年分数。利用这些值,该程序将检查学生是否有资格获得奖学金。

请参考按值调用和按引用调用以及函数文章,以了解按值传递和按引用传递之间的区别。在这里,我们将讨论第二种和第三种方法。

#include <stdio.h> 

struct Student
{
  char Student_Name[50];
  float First_Year_Marks;
  float Second_Year_Marks;
    
};

void PassBy_Value(struct Student Students);

int main()
{
  struct Student Student1;

  printf("\nPlease Enter the Student Name \n");
  scanf("%s",&Student1.Student_Name);
  
  printf("\nPlease Enter Student Inter First Year Marks \n");
  scanf("%f",&Student1.First_Year_Marks);
  
  printf("\nPlease Enter Student Inter Second Year Marks \n");
  scanf("%f",&Student1.Second_Year_Marks);
  
  PassBy_Value(Student1);   
  return 0;
}

void PassBy_Value(struct Student Students)
{
  float Sum, Average;

  Sum = Students.First_Year_Marks + Students.Second_Year_Marks;
  Average = Sum/2;
  
  if(Average > 900)
  {
    printf("\n %s is Eligible for Scholorship",Students.Student_Name); 
  }
  else
  {
    printf("\n %s is Not Eligible for Scholorship",Students.Student_Name); 
  }

}
Pass Structure to a Function By Value in C

在这个 C 语言结构体和函数的示例中,声明了 Student 结构体,其成员包括学生姓名、第一年分数、第二年分数,并使用了适当的数据类型。

C 编程的 main() 函数中,我们创建了 Student 结构体变量 Student1。

struct Student Student1;

接下来的 printf 和 scanf 语句用于要求用户输入学生姓名、第一年分数和第二年分数。

根据我们的示例,Student1 的值如下:

Student Name = Tutorialgateway.org;

First Year Marks = 800;

Second Year Marks = 750;

在下一行,我们调用了用户定义的函数

PassBy_Value(Student1);

当编译器到达这里时,它将向上查找。检查函数定义或 PassBy_Value (struct) 的声明。如果函数未能识别名为 PassBy_Value 的函数,则会抛出错误。

我们声明了两个 float 类型的局部变量 Sum 和 Average,用于计算每个学生第一年和第二年分数的总和与平均值。根据结果,该程序将判断学生是否有资格获得奖学金。

Sum = Students.First_Year_Marks + Students.Second_Year_Marks;

Sum = 800 + 750 = 1550

Average = Sum/2;

Average = 1550/2 = 775

在下一行,我们使用了If 语句来检查计算出的平均分是否大于 900。

  • 如果计算出的平均分大于 900,那么他有资格获得奖学金。
  • 如果计算出的平均分小于或等于 900,那么他没有资格获得奖学金。

注意:点运算符 (.) 用于赋值或访问结构体变量的值。

在 C 语言中按引用传递结构体给函数

在这个程序中,用户需要输入讲师姓名、总工作经验年限以及在本学院的工作经验年限。

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

struct Lecturer
{
  char Lecturer_Name[50];
  int Total_Experience;
  int Experience_In_This_College;
};

void PassBy_Reference(struct Lecturer *Lecturers);

int main() 
{
  struct Lecturer Lecturer1;
  
  printf("\nPlease Enter the Lecturer Name \n");
  scanf("%s",&Lecturer1.Lecturer_Name);
  
  printf("Please Enter Lecturers Total Years of Experience\n");
  scanf("%d",&Lecturer1.Total_Experience);
  
  printf("Enter Lecturers Total Years of Experience in this College\n");
  scanf("%d",&Lecturer1.Experience_In_This_College);
  
  PassBy_Reference(&Lecturer1);   
 
  printf("\n Lecturer Name = %s", Lecturer1.Lecturer_Name); 
  printf("\n Lecturers Total Years of Experience = %d", Lecturer1.Total_Experience); 
  printf("\n Years of Experience in this College = %d", Lecturer1.Experience_In_This_College); 
  
  return 0;
}

void PassBy_Reference(struct Lecturer *Lecturers)
{
  strcpy(Lecturers->Lecturer_Name, "Tutorial Gateway");
  Lecturers->Total_Experience = 5;
  Lecturers->Experience_In_This_College = 3;
}
Passing Structure to a Function By Reference

在这个 C 语言结构体和函数的示例中,声明了 Lecturer 结构体,其成员包括讲师姓名、讲师总工作经验年限以及讲师在本学院的总工作经验年限,并使用了适当的数据类型。

在 main() 函数中,我们创建了 Lecturer 结构体变量 Lecturer1。

struct Lecturer Lecturer1;

接下来的 printf 和 scanf 语句用于要求用户输入讲师姓名、讲师总工作经验年限以及讲师在本学院的总工作经验年限。根据我们的示例,Lecturer1 的值如下:

Lecturer Name = Suresh

Lecturers Total Years of Experience = 10

Lecturers Total Years of Experience = 6

在下一行,我们调用了用户定义的函数声明。

PassBy_Reference(&Lecturer1);

在这里,我们传递了 Lecturer1 结构体的地址,而不是它的副本。这意味着在函数内部对结构体成员所做的任何更改都会影响原始值。

在函数定义中,我们没有做任何特别的事情,只是为 Lecturer 结构体赋了新值。

strcpy(Lecturers->Lecturer_Name, "Tutorial Gateway");

Lecturers->Total_Experience = 5;

Lecturers->Experience_In_This_College = 3;

这意味着

Lecturer Name = Tutorial Gateway

Lecturers Total Years of Experience = 5

Lecturers Total Years of Experience = 3

由于我们将 Lecturer1 的地址传递给了 PassBy_Reference(),这些新值将替换用户输入的值。-> 运算符用于赋值或访问指针变量的值。