Title: CPS 196 Introduction to Computer Programming: C
1CPS 196Introduction to Computer Programming C
- Norka Lucena
- norka_at_ecs.syr.edu
- Summer Session II
- Syracuse University
- Tue August 03, 2004
2Overview
- More about structures
- Data structures
- Dynamic memory allocation
3Structures
- Data sets that allows us to associate different
data types as a whole entity - The data type itself is called structure and each
of its elements is called a data field - Structures can be single variables or arrays
- The reserved word struct is used to declare
structure - The . operator is used to access each data
field within the structure
4typedef Specifier
- A typedef declaration is a declaration that
creates a new type - Syntax
typedef char FlagType int main()
int myproc(int a) int FlagType
5Structures
include ltstdio.hgt define NAMELENGTH 25 define
NUMBEROFEMPLOYEES 5 typedef struct PayRecord
int id char nameNAMELENGTH double
rate PAYREC int main() int i PAYREC
employeesNUMBEROFEMPLOYEES 32479,
"Abrams, B.", 6.72, 33623, "Bohm, P.", 7.54
, 34145, "Donaldson, S.", 5.56, 35987,
"Enrst, T.", 5.34, 36203, "Gwodz, K.", 8.72
printf("ID\t\tName\t\tRate\n") printf("----
-------------------------------------------\n")
for(i 0 i lt NUMBEROFEMPLOYEES
i) printf("d\t\ts\t\t.2f\n",
employeesi.id, employeesi.name,
employeesi.rate) getchar() return(0)
6Passing and Returning Structures
- Individual structure members (data fields) are
passed to a function in the same way as any
basic-data type variable - Example
typedef struct PayRecord int id char
nameNAMELENGTH double rate double hours
PAYREC double calculatePay(double payRate,
double hours) int main() PAYREC
employee calculatePay(employee.rate,
employee.hours)
7Passing and Returning Structures
- Complete copies of the structure members are
passed using the correspondent structure type - Example
typedef struct PayRecord int id char
nameNAMELENGTH double rate double hours
PAYREC double calculateNet(PAYREC
employee) int main() PAYREC
employee calculateNet(employee)
8Passing and Returning Pointers to Structures
- Pointers to structures are passed using the
pointer notation already studied and the
correspondent structure type - Example
typedef struct PayRecord int id char
nameNAMELENGTH double rate double hours
PAYREC double calculateNet(PAYREC
employeePointer) int main() PAYREC
employee calculateNet(employee)
9-gt Operator
typedef struct PayRecord int id char
nameNAMELENGTH double rate double hours
PAYREC double calculateNet(PAYREC
employeePointer) int main() PAYREC
employee calculateNet(employee)
double calculateNet(PAYREC employeePointer)
double salary salary (employeePointer).r
ate (employeePointer).hours
double calculateNet(PAYREC employeePointer)
double salary salary employeePointer-gtrat
e employeePointer-gthours
10Time to recap!
11Data Structures
- Ways of organizing information for better
algorithm efficiency - Examples are queues, stacks, lists, and trees
root
head
leaf
tail
12List
- A sequence of cells
- Can grow and shrink on demand
- List elements can be accessed, inserted, or
deleted at any position within a list - Most popular types of lists are linear linked
list, linear doubly linked list, circular list,
circular doubly
13Types of Lists
14Time to recap!
15Dynamic Memory Allocation
- A technique that allows a program to allocate
memory at run time - In C is done with a set of functions from the
standard library stdlib - Dynamic memory allocation functions are
- malloc
- calloc
- realloc
- free
16Time to recap!