Structures and Functions in C

Structures and Functions in C : The C Programming allows us to pass the structures as the function parameters. Please refer to Functions in C Post before reading this post. It will help you to understand the concept of the function. We can pass the C structures to functions in 3 ways:

  1. Passing each item of the structure as a function argument. It is similar to passing normal values as arguments. Although it is easy to implement, we don’t use this approach because if the size of a structure is a bit larger, then our life becomes miserable.
  2. Pass the whole structure as a value.
  3. We can also Pass the address of the structure (pass by reference).

Structures and Functions in C Programming examples

The following examples will explain to you, How to Pass Structures to functions in C by value and By reference

Pass Structure to a Function By Value in C

If the structure is passed to the function by the value, then Changes made to the structure variable members within the function will not reflect the original structure members.

This program for Structures and Functions in C, User is asked to enter, Student Name, First Year Marks, and Second Year Marks. By using these values, this program will check whether the student is eligible for a scholarship or not.

Please refer Call By Value and Call By Reference to the Functions post to know the difference between pass by value and pass by reference. Here, we are going to discuss the second and third approaches

/* Passing Structure to a Function By Value */
#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); 
  }

}
Structures Pass By Value

In these Structures and Functions in C example, Declared the Student structure with Student Name, First Year Marks, Second Year Marks members with appropriate Data Types.

Within the C Programming main() function, we created the Student structure variable Student1

struct Student Student1;

Printf and scanf statements in the next lines used to ask the users to enter the Student Name, First Year Marks, Second Year Marks.

As per our example, the values for Student1 are as follows

Student Name = Tutorialgateway.org;

First Year Marks = 800;

Second Year Marks = 750;

In the next line, we called the user-defined function

PassBy_Value(Student1);

When the compiler reaches this function, then it will traverse to the top. And check for the function definition or else function declaration of the PassBy_Value (struct) function. If the function fails to identify the function with PassBy_Value name, then it will throw an error.

Function Definition

We declared 2 float local variables Sum and Average to calculate the Sum and average of every student’s first-year marks and second-year marks. Depending upon the result, this program will find whether the student is eligible for Scholarship or Not.

Sum = Students.First_Year_Marks + Students.Second_Year_Marks;

Sum = 800 + 750 = 1550

Average = Sum/2;

Average = 1550/2 = 775

In the next line, We used If statement to check whether the calculated average is greater than 900 or not

  • If the calculated Average is Greater than 900 then, He is Eligible for Scholarship
  • If the calculated Average is Less than or Equal to 900 then, He is Not Eligible for Scholarship

NOTE: Dot operator (.) is used to Assign or Access the values of Structure Variable

Passing Structure to a Function By Reference

In this Structure and Functions in C program, User asked to enter Lecturer Name, Total years of experience, and Total years of experience in this particular college.

/* Passing Structure to function by reference */
#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;
}
Please Enter the Lecturer Name 
Suresh
Please Enter Lecturers Total Years of Experience
10
Enter Lecturers Total Years of Experience in this College
6

 Lecturer Name = Tutorial Gateway
 Lecturers Total Years of Experience = 5
 Years of Experience in this College = 3

Within this Structure and Functions in C example, Declared the Lecturer structure with Lecturer Name, Lecturers Total Years of Experience, and Lecturers Total Years of Experience in this College member with appropriate Data Types.

Within the main() function, we created Lecturer structure variable Lecturer1

struct Lecturer Lecturer1;

Printf and scanf statements in the next lines used to ask the users to enter the Lecturer Name, Lecturers Total Years of Experience, and Lecturers Total Years of Experience in this College. As per our example, the values for Lecturer1 are as follows

Lecturer Name = Suresh

Lecturers Total Years of Experience = 10

Lecturers Total Years of Experience = 6

In the next line, we called the user-defined function

PassBy_Reference(&Lecturer1);

Here, We passed the address of the Lecturer1 structure to the function instead of its Clone. It means any changes made to the structural members within the function will reflect the original values.

Function Definition

Within the function, We haven’t done anything special, Just assigned new values to the Lecturer structure.

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

Lecturers->Total_Experience = 5;

Lecturers->Experience_In_This_College = 3;

It means

Lecturer Name = Tutorial Gateway

Lecturers Total Years of Experience = 5

Lecturers Total Years of Experience = 3

Since we passed the address of the Lecturer1 to the PassBy_Reference() function, these new values will replace the user entered values. Please refer Call By Value and Call By Reference. The -> operator is used to Assign or Access the values of Pointer Structure Variable