Concept
How do you pass an array of pointers to structures to a function?
ComputerScienceOne / Passing Arrays of Structures
"Nevertheless, as we’ve previously seen it is sometimes preferable to maintain an array of pointers to structures. If we had an array of pointers, we would have a function that looks 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\nThe only difference here is in how we access the gpa member variable using the arrow operator instead of the dot operator."
Related Ideas
- How can an array of structure pointers make swapping structures more efficient?ComputerScienceOne · Arrays of Structures
- How do you create and use an array of structures in C?ComputerScienceOne · Arrays of Structures
- How do you access members through a pointer to a structure?ComputerScienceOne · Selection Operators
- How are arrays passed to functions in C?ComputerScienceOne · Using Arrays with Functions
- How can you combine contiguous structure storage with indirect access?ComputerScienceOne · Arrays of Structures
- How should a comparator handle an array of structure pointers with NULL values?ComputerScienceOne · Sorting Pointers to Elements
- Why is it more efficient to sort pointers to structures?ComputerScienceOne · Sorting Pointers to Elements
- How can structures be dynamically allocated?ComputerScienceOne · Declaration & Initialization