Concept
How can you combine contiguous structure storage with indirect access?
ComputerScienceOne / Arrays of Structures
"It is also possible to take a “hybrid” approach to storing structures in arrays that stores them both contiguously, but also allows you to reference them indirectly through a pointer array. This is essentially what we did in Section 20.4.1 when we created a contiguous 2-dimensional array of integers. We made a single call to malloc and then had to setup the pointers to each “row.” To do this with structures, we can do something similar. First, we declare and allocate a dynamic array as before.\n\nint n = 10;\n\nStudent *rosterData = (Student *) malloc(n * sizeof(Student));"
Related Ideas
- How do you create and use an array of structures in C?ComputerScienceOne · Arrays of Structures
- How can pointer manipulation create a contiguous two-dimensional array?ComputerScienceOne · Contiguous 2-D Arrays
- Why use a contiguous memory block for a two-dimensional array?ComputerScienceOne · Contiguous 2-D Arrays
- How do you pass an array of pointers to structures to a function?ComputerScienceOne · Passing Arrays of Structures
- How do you pass an array of structures to a function to compute an average GPA?ComputerScienceOne · Passing Arrays of Structures
- Why is it more efficient to sort pointers to structures?ComputerScienceOne · Sorting Pointers to Elements
- How can structures be dynamically allocated?ComputerScienceOne · Declaration & Initialization
- How does malloc() allocate memory for a dynamic array?ComputerScienceOne · Dynamic Memory