You are on page 1of 15

CMSC 21

Fundamentals of programming

Kristine Bernadette P. Pelaez

Institute of Computer Science


University of the Philippines Los Baños
Defining
structures
structures are usually
defined outside any function.
Defining
structures
structures are usually
defined outside any function.
defining a structure is like
creating your own data type.
Defining
structures
structure definitions are just definitions;
you cannot use them unless you
declare a structure variable.
Structure Variable
Declaration
If you used structure tags:
struct <structure_tag> <struct_varname>;

If you used type definition:


<synonym> <struct_varname>;
Initialization of
Structure Variables

similar to the initialization of a static array


<type> <struct_varname> = {member1,member2,…,memberN};
Accessing Members
of a Structure Variable
done using the dot operator
Accessing Members
of a Structure Variable
done using the dot operator

varname.memberName
Structure Pointer

you can also declare


a pointer to a structure
Structure Pointer

you can also declare


a pointer to a structure

<struct_type> *<varname>;
Accessing Members
using a Structure Pointer
How will you access
pointer the members of the
structure using the
pointer?

member1 ... memberN


Accessing Members
using a Structure Pointer
(*ptr).member
pointer

member1 ... memberN


Accessing Members
using a Structure Pointer
ptr->member
pointer
arrow
operator

member1 ... memberN


Passing Structures
as Parameters
Pass by Value and
Pass by Reference
of a structure is just the
as those of normal variables.
CMSC 21
Fundamentals of programming

Kristine Bernadette P. Pelaez

Institute of Computer Science


University of the Philippines Los Baños

You might also like