Chapter 8: Structure - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 8: Structure

Description:

Chapter 8: Structure & Enumeration In this chapter you will learn about, Introduction Defining a structure Declaring a structure variable Initialising structure elements – PowerPoint PPT presentation

Number of Views:151
Avg rating:3.0/5.0
Slides: 24
Provided by: Nazrita
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8: Structure


1
Chapter 8 Structure Enumeration
  • In this chapter you will learn about,
  • Introduction
  • Defining a structure
  • Declaring a structure variable
  • Initialising structure elements
  • Acessing structure elements
  • Examples using structure
  • Defining enumeration constants
  • Declaring enumeration variable
  • Examples using enumeration

2
Introduction
  • So far we have only used
  • Basic data types, such as int, float, double and
    char.
  • Array
  • String (which is an array of characters)
  • The concept of structure is similar to array in
    the sense that both store a group of elements.
  • However, in an array, all the elements are of the
    same data type but in a structure, the data can
    be of different data types.
  • A structure can contain basic data types, arrays
    and even another structure.

3
Defining a Structure
  • General syntax
  • struct structure_name
  • data_type element1
  • data_type element2
  • . . .
  • Example
  • struct student
  • char name20
  • int studentID
  • char major50

4
Declaring Structure Variables
  • After defining a structure, we may declare
    variables that are of that structure. A
    structure variable declaration requires
  • The keyword struct
  • The structure type name
  • A list of elements (variable names) separated by
    commas
  • A concluding semicolon
  • For example, to declare a variable my_student of
    type structure student, it should be written as
    follow.
  • struct student my_student
  • struct student my_stud, your_stud

5
Declaring Structure Variables
  • By including this declaration in our program, we
    are informing the compiler about a new structure
    defined by the user.
  • The declaration makes the compiler aware of the
    existence of the structure does not take an
    action yet.
  • As a result of the declaration, sufficient memory
    space for variable my_student, which includes the
    components of its structure, will be reserved.

6
Declaring Structure Variables
Conceptual memory structure variable my_student
7
Declaring Structure Variables
  • It is possible to combine the declarations of a
    structure and its variable by including the name
    of the variable at the end of the structure
    declaration.
  • struct student
  • char name20
  • int studentID
  • char major50
  • struct student my_student

struct student char name20 int
studentID char major50 my_student
?
8
Nested Structure
  • It is possible for a structure to have another
    structure as one of its components/elements.
  • Consider the following structure declaration
  • struct address
  • int no
  • char street20
  • int zipcode

9
Nested Structure
  • We can rewrite the structure student declaration
    to include structure address as one of its
    elements as follow.

struct student char name20 int
studentID char major50 struct address
addr my_student
10
Declaring Nested Structure cont
  • The updated structure student can also be written
    as below.
  • struct student
  • char name20
  • int studentID
  • char major50
  • struct address
  • int no
  • char street20
  • int zipcode
  • addr
  • my_student

11
Accessing Structure Elements
  • A structure contains many elements. Each element
    of a structure can be accessed using the
    structure variable name followed by the . (dot)
    operator followed by the element name.
  • Let us use the structure student which we have
    seen before as an example.

struct student char name20 int
studentID char major50 my_student
12
Accessing Structure Elements
  • Therefore, to access the elements of a
    my_student, we write as follow
  • my_student.name
  • my_student.studentID
  • my_student.major
  • Therefore, to assign values to the element of the
    structure, we write
  • my_student.studentID 10179

13
Initialising Structure Elements
  • Just like array and other types of variables, we
    can initialise a structure while we are declaring
    an instance of the structure. For example,
  • struct student my_student "Ahmad", 10179,
    "IT"
  • Notice that it is possible to use the
    operator on a struct variable. When the sign
    is used, each value on the right will be
    respectively copied into the structure components
    according to their declaration in the structure
    definition.

14
Example 1
  • struct birthdate
  • int mon
  • int day
  • int yr
  • struct birthdate me 10, 25, 1881
  • printf("I was born on d/d/d\n", me.day,
    me.mon, me.yr)

15
Example 2
16
Array of structures
includeltstdio.hgt struct birthdate int
mon int day int yr void
main(void) struct birthdate me5 for (i0
ilt5 i) printf("Enter day") scanf("d",
mei.day) printf("Enter month") scanf("d",
mei.mon) printf("Enter year") scanf("d",
mei.yr)
17
Enumeration Constants
  • An enumeration, introduced by the keyword enum,
    is a set of integer constants represented by
    identifiers.
  • Example definition
  • enum islamic_months
  • muharam, safar, rabiulawal, rabiulakhir,
  • jamadilawal, jamadilakhir, rejab, syaaban,
  • ramadhan, syawal, zulkaedah, zulhijjah
  • Each of the identifiers will have a corresponding
    integer constant value that starts with 0 (unless
    specified otherwise). Therefore, we can treat
    them as integers.

18
Enumeration Constants
  • If we want the enumeration to start with a value
    other than 0, we can assign the value to the
    first identifier
  • enum islamic_months
  • muharam 1, safar, rabiulawal, rabiulakhir,
  • jamadilawal, jamadilakhir, rejab, syaaban,
  • ramadhan, syawal, zulkaedah, zulhijjah
  • Just like structure, before an enumeration can be
    used, an enumerated variable needs to be
    declared
  • enum islamic_months my_months

19
Enumeration Constants
  • Enumeration constants basically makes your
    program more readable.
  • There are cases where it is appropriate for us to
    use an enumeration. For example,
  • enum islamic_months my_months
  • scanf("d", my_months)
  • switch (my_months)
  • case muharam
  • . . .
  • break
  • case safar
  • . . .
  • break
  • case ramadhan printf(lunch not provided)
  • break

20
Enumeration Constants
  • This is another case where it is more appropriate
    to use an enumeration.
  • enum Boolean FALSE, TRUE
  • void main ( )
  • int number 5, key
  • Boolean found
  • printf("Enter number ")
  • scanf("d", key)
  • if (number key)
  • found TRUE
  • else
  • found FALSE

21
Example
include ltstdio.hgt enum months JAN 1, FEB,
MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV,
DEC void main ( ) enum months month char
monthsName "January","February","March","Ap
ril","May","June", "July","August","September","O
ctober","November", "December" for (month
JAN month lt DEC month) printf("d s",
month, monthNamemonth-1)
22
Example
  • Output
  • 1 January
  • 2 February
  • 3 March
  • April
  • May
  • June
  • July
  • August
  • September
  • October
  • November
  • December

23
Summary
  • This chapter you learned about structures
  • Structure definition and structure variable
    declaration
  • Initialising structure elements
  • Accessing structure elements
  • Defining, declaring and using enumeration
    constants
Write a Comment
User Comments (0)
About PowerShow.com