CPS 196 Introduction to Computer Programming: C - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CPS 196 Introduction to Computer Programming: C

Description:

Introduction to Computer Programming: C. Norka Lucena. norka_at_ecs.syr.edu. Summer Session II ... CPS196 - Summer Session II - 2004. Passing and Returning ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 17
Provided by: su15
Category:

less

Transcript and Presenter's Notes

Title: CPS 196 Introduction to Computer Programming: C


1
CPS 196Introduction to Computer Programming C
  • Norka Lucena
  • norka_at_ecs.syr.edu
  • Summer Session II
  • Syracuse University
  • Tue August 03, 2004

2
Overview
  • More about structures
  • Data structures
  • Dynamic memory allocation

3
Structures
  • 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

4
typedef Specifier
  • A typedef declaration is a declaration that
    creates a new type
  • Syntax

typedef char FlagType int main()
int myproc(int a) int FlagType
5
Structures
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)
6
Passing 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)
7
Passing 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)
8
Passing 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
10
Time to recap!
11
Data Structures
  • Ways of organizing information for better
    algorithm efficiency
  • Examples are queues, stacks, lists, and trees

root
head
leaf
tail
12
List
  • 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

13
Types of Lists
14
Time to recap!
15
Dynamic 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

16
Time to recap!
Write a Comment
User Comments (0)
About PowerShow.com