Chapter 19 - Data structures. After studying this chapter you will be able to understand: Structures in C, defining a struct, declaring and using a struct, defining and declaring at once, using typedef, generating code for structs, array of structs, pointer to struct,. | Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together. We want to organize these data bundles in a way that is convenient to program and efficient to execute. An array is one kind of data structure. In this chapter, we look at two more: struct – directly supported by C linked list – built from struct and dynamic allocation 19- Structures in C A struct is a mechanism for grouping together related data items of different types. Recall that an array groups items of a single type. Example: We want to keep track of weather data for the past 100 days. For each day, we can define the following data: int highTemp; int lowTemp; double precip; double windSpeed; int windDirection; We can use a struct to group these data together for each day. 19- Defining a Struct We first need to define a new type for the compiler and tell it what our struct looks like. struct w_type { int highTemp; int lowTemp; double precip; double windSpeed; int windDirection; }; This tells the compiler how big our struct is and how the different data items (“members”) are laid out in memory. But it does not allocate any memory. 19- Declaring and Using a Struct To allocate memory for a struct, we declare a variable using our new data type. struct w_type day; Memory is allocated, and we can access individual members of this variable: = 32; = 22; A struct’s members are laid out in memory in the order specified by the struct definition. 32 22 19- Defining and Declaring at Once You can both define and declare a struct at the same time. struct w_type { int highTemp; int lowTemp; double precip; double windSpeed; int windDirection; } day; And you can still use the w_type name to declare other structs. struct w_type day2; 19- typedef C provides a way to define a data type by giving a new name | Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together. We want to organize these data bundles in a way that is convenient to program and efficient to execute. An array is one kind of data structure. In this chapter, we look at two more: struct – directly supported by C linked list – built from struct and dynamic allocation 19- Structures in C A struct is a mechanism for grouping together related data items of different types. Recall that an array groups items of a single type. Example: We want to keep track of weather data for the past 100 days. For each day, we can define the following data: int highTemp; int lowTemp; double precip; double windSpeed; int windDirection; We can use a struct to group these data together for each day. 19- Defining a Struct We first need to define a new type for the compiler and tell it what our struct looks like. struct w_type { int highTemp; int