Ch9 - CSEB114:PrincipleofProgramming...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style prepared by NI, edited by CSEB114: Principle of Programming Chapter 9: User Defined Data Types
Background image of page 1

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

View Full DocumentRight Arrow Icon
prepared by NI, edited by Objectives In this chapter you will learn about, Introduction Declaring Structure Type & Structure Variables Referring and initializing structure elements Passing structures to a function Using typedef Example using structure Enumeration constants
Background image of page 2
prepared by NI, edited by Introduction So far we have only used data types which have been defined by C such as int, double and char. It is also possible to create our own data types. A user defined data type is called a structure . A structure can contain both built-in data types and another structure. The concept of structure is pretty much the same as arrays except that in an array, all the
Background image of page 3

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

View Full DocumentRight Arrow Icon
prepared by NI, edited by Definition A structure is a derived data type that represents a collection of a related data items called components (or members) that are not necessarily of the same data type.
Background image of page 4
prepared by NI, edited by Declaring Structure Type  General syntax: struct structure_name { data_type element1; data_type element2; . . . }; Example: struct student { char name[20]; int studentID; Also called as structure tag Components / members
Background image of page 5

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

View Full DocumentRight Arrow Icon
prepared by NI, edited by Declaring Structure Variables After declaring a structure type, we may declare variables that are of that type. A structure variable declaration requires: The keyword struct The structure type name A list of members (variable names) separated by commas A concluding semicolon Then, assume that variable of structure type
Background image of page 6
prepared by NI, edited by Based on example: struct student By including this declaration in our program, we are informing the compiler about a new data type which is a user defined data type. The declaration just makes the compiler aware the existent of new data type but does not take an action yet. Based on the declaration of struct student my_student; causes the compiler to reserve memory space for variable my_student and the components of its
Background image of page 7

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

View Full DocumentRight Arrow Icon
prepared by NI, edited by Based on example: struct student Structure variable Components Values name major studentID Simon 0078 CS my_student Conceptual memory structure variable my_student of type student (assuming that the components of variable my_student have already been assigned values)
Background image of page 8
prepared by NI, edited by Based on example: struct student It is possible to combine the declarations of a structure type and a structure variable by including the name of the variable at the end of the structure type declaration. struct student { char name[20]; int studentID; char major[50];       };        struct student my_student; struct student { char name[20]; int studentID; char major[50];      } my_student; =
Background image of page 9

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

View Full DocumentRight Arrow Icon
prepared by NI, edited by Declaring Nested Structure
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

Ch9 - CSEB114:PrincipleofProgramming...

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

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