TAILIEUCHUNG - Lecture Introduction to computing systems (2/e): Chapter 19 - Yale N. Patt, Sanjay J. Patel

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

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.