Week-13 Struct Enum Typedef

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Enum, Typedef & Structs

Assist. Prof. Dr. Umit ATİLA


Enum
● Enumaration constants are like symbolic
constants.
– Enum values are set automatically
– Values start at 0 and incremented by 1
– Need unique constant names
● We can create our own data type by enum
● For example we can create a new boolean type
in which 0 is false and 1 is true
Enum
Enum
Enum
● Notice that we use enum keyword for each variable definition.
● We have two alternative ways for not writing enum for each variable
definition
– Defining variable with enum definition
– Using typedef
● Enum is useful if you have some data to be grouped or arranged.
Some examples:
– enum education { primarySchool, secondarySchool, highSchool, graduate,
master };
– enum education student;
– enum sex { male, female };
– enum sex person;
Enum
Typedef
● Used to name data types with other user
defined names.
● Format of typedef:
– typedef old_datatype_name new_datatype_name
– typedef int tamsayi
● defines int as tamsayi
Typedef
Enum
● If enum is defined globally it can be used as parameter to a function
Enum
Structures
● Used to group different types of variables in one
structure.
● Structures are important for object oriented
programming
Structures
Structures

● If not used struct in this sample we would have to define 9 different variables.
● We make it with 3 variables.
● Think about a program that takes 20 informations for a person.
● With 3 person you have to define 60 different variables.
● Another advantage using struct is with copying data on an other variable.
● you = yourSister assignment copies your sister's data on to your data
Nested Structures
Labeling Structures

● Labeling structures has many advantages.


● If dont, you can just define variables when defining struct.
● If you use labels you can define as many variables as you want from struct in any point
of your program
● We have to create a variable using label to start using structure
Initial Values for Structures
● Structures can be defined with initial values of
its variables.
● Order of the values must fit to the structure's
order.
● You can give initial values for structures defined
with or without label.
Initial Values for Structures
Arrays of Structures
Accessing Structures with Pointers
Passing Structures to Functions
● Define structure globally and pass to function

You might also like