You are on page 1of 21

CMSC 21

Fundamentals of programming

Kristine Bernadette P. Pelaez

Institute of Computer Science


University of the Philippines Los Baños
Structures
Structures

collection of related data


Structures

collection of related data that


may be of different types
Structures

name level healthpts

pokemon

name GWA age

student
Structures

char name[30] int level int healthpts

pokemon

char name[50] float GWA int age

student
Structures

each item that belongs to a


structure is called a member of
that structure
Structures

char name[30] int level int healthpts

pokemon

char name[50] float GWA int age

student
Defining
structures
there are three (3) ways
to define a structure in C:
1. using structure tags
2. using type-definition
3. using both
Tagged
Structures
struct <structure_tag>{
<member1>;
<member2>; struct keyword

<memberN>;
};
Tagged
Structures
struct <structure_tag>{
<member1>;
<member2>; structure tag
… acts as the name of
<memberN>; your structure
};
Tagged
Structures
struct <structure_tag>{
<member1>;
<member2>;

<memberN>;
};

char name[30] int level int healthpts

pokemon
Type-defined
structures
typedef struct{
<member1>;
<member2>; typedef &

<memberN>; struct keyword
}<synonym>;
Type-defined
structures
typedef struct{
<member1>;
<member2>; synonym
… acts as the name of
<memberN>; your structure
}<synonym>;
Type-defined
structures
typedef struct{
<member1>;
<member2>;

<memberN>;
}<synonym>;

char name[30] int level int healthpts

pokemon
Type-defined &&
Tagged structures
typedef struct <structure_tag>{
<member1>;
<member2>; typedef &

<memberN>; struct keyword
}<synonym>;
Type-defined &&
Tagged structures
typedef struct <structure_tag>{
<member1>;
<member2>; structure tag
… acts as the name of
<memberN>; your structure
}<synonym>;
Type-defined &&
Tagged structures
typedef struct <structure_tag>{
<member1>;
<member2>; synonym
… acts as the “synonym”
<memberN>; for name of your
}<synonym>;
structure
Type-defined &&
Tagged structures
typedef struct <structure_tag>{
<member1>;
<member2>;

<memberN>;
}<synonym>;

char name[30] int level int healthpts

pokemon
Type-defined &&
Tagged structures
struct <structure_tag>{
<member1>;
<member2>;

<memberN>;
};

typedef struct <structure_tag> <synonym>;


CMSC 21
Fundamentals of programming

Kristine Bernadette P. Pelaez

Institute of Computer Science


University of the Philippines Los Baños

You might also like