1. Trang chủ
  2. » Khoa Học Tự Nhiên

Thuật toán trong Structures

17 130 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 17
Dung lượng 129,9 KB

Nội dung

Structures and Enumerators Structures • Structure: allows multiple variables of different types to be grouped together = User Defined Type • Structure Declaration Format: struct structure name { type1 field1; type2 field2; … typen fieldn; }; 7-2 Example struct Declaration struct Student { int studentID; string name; short year; double grades[10]; }; structure tag structure members Notice the required ; 7-3 Defining Structure Variables • struct declaration does not allocate memory or create variables • To define variables, use structure tag as type name Student s1; s1 studentID name year grades In C it is necessary to use the ‘struct’ keyword when declaring struct variables: struct Student s1; Or use a typedef to avoid this: typedef struct { … } Student; 7-4 Structure Initialization Structures can be initialized with a comma separated list of values, similar to arrays: Structure Declaration struct Dimensions { int length, width, height; }; Structure Variable box length 12 width height Dimensions box = {12,6,3}; 7-5 Accessing Structure Members • Use the dot (.) operator to refer to members of struct variables Student student1; getline(cin, student1.name); cin >> student1.studentID; student1.grades[0] = 3.75; 7-7 Displaying struct Members To display the contents of a struct variable, you must display each field separately, using the dot operator Wrong: cout

Ngày đăng: 27/08/2017, 14:32

TỪ KHÓA LIÊN QUAN

w