Bifalgorithm post · Computer science one · Arrays of Structures

How do you create and use an array of structures in C?

How do you create and use an array of structures in C? (Computer science one · Arrays of Structures) — "Just as we can create arrays of built-in types such as…

Ideas in this flowchart

  • Arrays can store user-defined structures such as Student records.
  • An array of structures can be dynamically allocated like an array of built-in types.
  • The allocation creates space for 10 Student structures: roster = (Student *) malloc(sizeof(Student) * 10).
  • The structure array can be used like any other array.
  • Indexing roster produces a regular Student structure, whose members are accessed with the dot operator.
  • The GPA values can be accumulated by indexing each Student and accessing roster[i].gpa.
  • The average GPA is computed by dividing the accumulated sum by 10.
  • Each array element occupies sizeof(Student) bytes.
  • Structure elements can be reassigned or swapped like ordinary variables.
  • The first two elements can be swapped through a temporary Student variable.
  • Assigning or swapping a structure copies every byte that makes up the structure.
  • Copying entire structures may become costly for large structures, frequent operations, or function calls.
  • A Student structure is estimated to occupy 40 bytes.
  • The structure size may vary across systems and compilers because of alignment and padding.
  • With this approach, structure instances are stored contiguously in memory.
  • Swapping two records can therefore copy two entire 40-byte memory blocks.
Feed/

How do you create and use an array of structures in C?

No cover

Channel

Computer science one

1 page

"Just as we can create arrays of built-in types such as integers, we can also create arrays of our user-defined structures. As an example, the following creates an array of 10 Student structures. Once created, we can treat them like any other array.\n\nStudent *roster = (Student *) malloc(sizeof(Student) * 10);\n\n...\n\ndouble sum = 0.0;\n\nfor(i=0; i<10; i++) {\n\nsum += roster[i].gpa;\n\n}\n\ndouble averageGpa = sum / 10;\n\nAs in the example, we can index each element in the array, roster. Once indexed, each element is a regular structure and so we use the dot operator to access each of its member variables. As with any other array, each element takes up a number of bytes, equal to sizeof(Student). We can swap and reassign each element just like any other variable. For example, the following code swaps the first two elements using a temporary variable.\n\nStudent temp = roster[0];\n\nroster[0] = roster[1];\n\nroster[1] = temp;\n\nEach of these operations copies over every byte that makes up the structure. For small structures, this isn’t that big of a deal. However, for larger structures, this may become an issue, especially if we do this often or pass structures around to functions. As presented, each Student structure takes 40 bytes. This is just an estimate and may vary on different systems and compilers. Usually, compilers use alignment and may pad structures with extra bytes in order to make it more efficient to store in memory. With the first approach, each structure instance is stored contiguously in memory. Swapping two records involves copying entire blocks of 40 bytes."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.