cmpt130_10structures

cmpt130_10structures - Structures Structures Complex data...

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

View Full Document Right Arrow Icon
Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
Structures Complex data types Structures Defined types Structures and functions Structures and pointers
Background image of page 2
Representing Data Many programs require complex data to be represented That cannot easily be represented using base type variables and arrays C allows structures to be defined and used in a program A structure is a complex type that collects a number of variables, arrays or other strucures
Background image of page 3

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

View Full DocumentRight Arrow Icon
Representing Students Let’s assume that we want to represent student data for a small university For each student we want to maintain First name – a string Last name – a string Student number – an int GPA – a float this is a simplified version of what would be necessary, some of the complicating issues will be discussed later
Background image of page 4
Students without Structures If we wanted to maintain a list of hundreds of students we could use four separate arrays One for each of the four attributes First name, last name, student number, GPA Referred to as parallel arrays Whenever we want to retrieve student data we would use the same index in all arrays
Background image of page 5

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

View Full DocumentRight Arrow Icon
Parallel Array Drawbacks Maintaining parallel arrays can be hard We must make consistent changes to all arrays If we delete an element from one, elements at the same index must be deleted from the others If we sort one array then all the other arrays must be sorted in the same way Passing student data to functions is tedious Functions that require student data must be passed four values
Background image of page 6
Structure Declarations A structure declaration is essentially the definition of a complex type struct student { int id; char first[MAXNAME]; char last[MAXNAME]; float gpa; }; keyword optional tag student variable s the tag is optional but if we are going to declare the struct in one place and use it in another we need a tag
Background image of page 7

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

View Full DocumentRight Arrow Icon
Defining a Structure Variable A structure declaration describes what data types are associated with a struct It does not allocate space for any variables Space is allocated when a variable is defined struct student s1; Instructs the compiler to reserve space for an int, two character arrays and a float The four components of s1 are allocated memory
Background image of page 8
Tag-less struct s A struct can be declared without a tag If a variable is defined following the declaration struct { int id; char first[MAXNAME]; char last[MAXNAME]; float gpa; } s1;
Background image of page 9

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

View Full DocumentRight Arrow Icon
What Goes Where It is common for struct definitions to appear outside any function So that they are available anywhere in the file If a struct is defined inside a function it is only available in that function Variables of a struct type are declared wherever they are needed Just like any other variable
Background image of page 10
Initializing a Structure
Background image of page 11

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

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

This note was uploaded on 12/23/2011 for the course CMPT 120 taught by Professor Cuikerman during the Fall '08 term at Simon Fraser.

Page1 / 36

cmpt130_10structures - Structures Structures Complex data...

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

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