7_pdfsam_cs2022_lectures - Structures Structures aggregate...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Structures Structures aggregate variables of different types Like classes in Java, but they can only store data Defined with the keyword struct // define new structure struct Student { int id, year; char grade; }; int main() { struct Student s; s.id = 10001; s.year = 2010; s.grade = ‘B’; printf(“%d %d %c\n”, s.id, s.year, s.grade); } (int) id (int) year (char) grade struct student 0 4 8 struct student requires approximately 9 bytes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Structure syntax To define a structure To declare a variable of the new structure type The struct keyword is required in both places struct structure_name { /* list of variable declarations */ }; int main() { struct structure_name variable_name; /* code */ }
Background image of page 2
Structures and typedef To avoid the struct keyword, use a typedef create alias for struct Student_struct called Student // define new structure struct Student_struct { int id, year; char grade; }; typedef struct Student_struct Student ; int main() { Student s; s.id = 10001; s.year = 2010; s.grade = ‘B’; printf(“%d %d %c\n”, s.id, s.year, s.grade); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

7_pdfsam_cs2022_lectures - Structures Structures aggregate...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online