Skip to main content
Logo image

Section 25.4 Passing Structures to Functions

Subsection 25.4.1 Passing by Value

In the next video we’ll learn how to pass structures to functions by value.
Passing by value means that a copy will be made of the data stored in the structure. We will therefore have access to all of the data stored in the structure variable but we cannot modify it within the function (since all we’d end up doing is modify the copies of the values).

Investigate 25.10.

Imagine we declare a structure named student in both the main() function and in a new function, and we then pass the structure from the main() function into our new one by value. If we now change a value within the new function’s structure, will it affect the original structure?
We’ll work with the following structure example:
struct student{
  char firstName[30];
  char lastName[30];
  int birthYear;
  double aveGrade;
};
This stores student data (first and last name, birth year and average grade) in a structure.
The following Codecast shows you how to pass a student record to a function in order to print the record. Note that in the video we do not use typedef for the sole reason that sadly, typedef has not (yet) been implemented in the recording software.

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.
    Suppose you have declared a structure to hold student data as in the video:
    struct student{
      char firstName[30];
      char lastName[30];
      int birthYear;
      double aveGrade;
    };
    
    Suppose furthermore that you are writing a function compareStudents(), to which you’d like to pass two students of type struct student, and which returns the average grade of the student whose average grade is the lower of the two. Which of the following is the correct function prototype for such a function?
  • double compareStudents(struct student stud1, struct student stud2);
  • Correct
  • struct student compareStudents(struct student stud1, struct student stud2);
  • Not quite. Try again!
  • double compareStudents(stud1, stud2);
  • Not quite. Try again!
2.
    In the same scenario as in the previous question, where you have declared a structure to hold student data via:
    struct student{
      char firstName[30];
      char lastName[30];
      int birthYear;
      double aveGrade;
    };
    
    and are writing a function compareStudents() which compares the average grades of the two student records passed to the function, which of the following correctly finds the lower of the two average grades:
  • double lower;
    if (stud1.aveGrade<stud2.aveGrade)
      lower = stud1.aveGrade;
    else
      lower = stud2.aveGrade;
    
  • Correct
  • double lower;
    if (aveGrade(stud1)<aveGrade(stud2))
      lower = stud1.aveGrade;
    else
      lower = stud2.aveGrade;
    
  • Not quite. Try again!
  • double lower;
    lower = stud1.aveGrade<stud2.aveGrade;
    
  • Not quite. Try again!

Subsection 25.4.2 Passing by Reference

If we want to be able to modify a structure variable from within a function we need to pass a pointer to this variable to the function - just as we do for regular variables. In other words, we need to pass the variable to the function "by reference". In the next video we’ll learn how to do so.

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.
    What is wrong with the following piece of code and how would you fix it?
    struct student{
      char firstName[30];
      char lastName[30];
      int birthYear;
      double aveGrade;
    };
    
    int main(void) {
      struct student me = {"Petra", "Bonfert-Taylor", 2001, 3.8};
      struct student * studentptr = &me;
    
      *studentptr.birthYear = 1998;
      return 0;
    }
    
  • The second-to-last line of code should be (*studentptr).birthYear = 1998;
  • Correct
  • The second-to-last line of code should be (&studentptr).birthYear = 1998;
  • Not quite. Try again!
  • The second-to-last line of code should be (*studentptr.birthYear) = 1998;
  • Not quite. Try again!
  • There is nothing wrong with this code.
  • Not quite. Try again!