struct_wrapper_dynamic

The following example shows how to create a wrapper for a dynamically allocated array. This simplifies array access and size management – i.e. how many elements are stored, for how many is there room for, where is the data?

For testing, an example file with workflow is provided, that can be used for the program’s input stdin :

  1. An array that can hold two members is created
  2. Data for two members is entered
  3. An attempt is made to extend the array by adding three more members.

The result of the third step depends on whether EXPANDABLE  macro is set to 1 or 0.

To test with the provided data using stream redirection: ./program_name < test_data.txt

The following code is given as source and header file. The previously mentioned  EXPANDABLE  macro is in the source file. When setting the value to 1, realloc()  is used to expand the dynamic array . If it is set to 0, the structure size is fixed by the first prompt.

Expanding the array is done using (n * 2)  method for reallocation.