Concept
How do you pass an array of structures to a function to compute an average GPA?
ComputerScienceOne / Passing Arrays of Structures
"We often also have need to pass arrays of structures to functions. As an example, consider passing an entire roster of students to a function in order to compute the average GPA. If we have an array of structures, we would have a function that looked something like the following.\n\n/**\n\n* Computes the average GPA of the Student structures in\n\n* the given roster (which is of size n).\n\n*/\n\ndouble computeAverageGpa(const Student *roster, int n) {\n\ndouble sum = 0.0;\n\nint i;\n\nfor(i=0; i<n; i++) {\n\nsum += roster[i].gpa;\n\n}\n\nreturn sum / n;\n\n}\n\nWhen we pass in the array of structures, it is not passed by value. That is, the total number of bytes for each student is not copied onto the call stack."
Related Ideas
- How are arrays passed to functions in C?ComputerScienceOne · Using Arrays with Functions
- How do you create and use an array of structures in C?ComputerScienceOne · Arrays of Structures
- How can an array of structure pointers make swapping structures more efficient?ComputerScienceOne · Arrays of Structures
- How can you combine contiguous structure storage with indirect access?ComputerScienceOne · Arrays of Structures
- How are generic getMax() functions used with integers and structures?ComputerScienceOne · Function Pointers
- How does malloc() allocate memory for a dynamic array?ComputerScienceOne · Dynamic Memory
- Why is it more efficient to sort pointers to structures?ComputerScienceOne · Sorting Pointers to Elements
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions