cmpt130_10structures

cmpt130_10structures - Structures Structures Complex data...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Structures
Image of page 1

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

View Full Document Right Arrow Icon
Structures Complex data types Structures Defined types Structures and functions Structures and pointers
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
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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 on the stack in sequence
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; This is really only useful for one-off struct s
Image of page 9

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

View Full Document Right 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
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern