STRUCTURES - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

STRUCTURES

Description:

provides a means to aggregate variables of different types ... tmp.grade= A'; tmp.last_name='Casper'; tmp.st_id=1000; printf('%dn',tmp.grade) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 17
Provided by: skum1
Category:

less

Transcript and Presenter's Notes

Title: STRUCTURES


1
STRUCTURES
  • CHAPTER 9

2
Structures provides a means to aggregate
variables of different types
  • EXAMPLE You have a file including the records
    below
  • ID NAME GRADE
  • 12 Joe A
  • 17 Mary B
  • 89 Sue A
  • 10 Tom C
  • int id4 char names4 char grade4
  • id012 names1Joe grade0A

3
Structures
record is the tag name
struct record int ID char name char
grade struct record s1 struct record
s2 s1.ID12 s1.nameJoe s1.gradeA s2.ID1
7 s2.nameMary s2.gradeB
  • ID NAME GRADE
  • 12 Joe A
  • 17 Mary B
  • 89 Sue A
  • 10 Tom C

struct record is the new type
4
Structures
struct record int ID char name char grade
struct record s1 struct record s2
s1
ID
name
grade
s2
ID
name
grade
5
Structures
  • struct record
  • int ID char name
  • char grade
  • struct record s4
  • s0.ID12
  • s0.nameJoe
  • s0.gradeA
  • s1.ID17 s1.nameMary
  • s1.gradeB
  • s3s1

ID
name
grade
s0 ?
ID
name
grade
s1 ?
ID
name
grade
s2 ?
s3 ?
ID
name
grade
ID NAME GRADE 12 Joe A 17 Mary B 89
Sue A 10 Tom C
6
Structures DECLARATION ALTERNATIVES
  • Declaration 1
  • struct record int ID char name char
    grade
  • struct record s1
  • struct record s2
  • struct record s3
  • Declaration 2
  • struct record int ID char name char
    grade s1, s2
  • struct record s3

7
Structures DECLARATION ALTERNATIVES
  • Declaration 1
  • struct record
  • int ID char name char grade
  • struct record s1
  • struct record s2
  • Declaration 3
  • struct
  • int ID char name char grade s1, s2
  • / no tag name /
  • / no permission to declare other variables of
    this type /

8
Structures DECLARATION ALTERNATIVES
  • Declaration 4
  • struct record int ID char name char
    grade
  • typedef struct record rec
  • rec s1
  • struct record s2
  • Declaration 5 / high degree of modularity and
    portability /
  • typedef struct int ID char name char
    grade rec
  • rec s1
  • rec s2

9
INITIALIZATION
  • struct names
  • char name10
  • int length
  • int weigth man3 Tom,180,65,
  • George, 170,68,
  • Bob, 190,100
  • struct names woman2Mary, 170, 55, Sue,
    160,67
  • struct names your
  • your. nameJane
  • your.length160
  • your.weigth50

10
INITIALIZATION
  • includeltstdio.hgt
  • struct physical_info
  • int length
  • int weigth
  • struct record
  • int salary
  • int working_hour
  • struct physical_info man
  • main()
  • struct record s1
  • s1.salary10000
  • s1.working_hour 6
  • s1.man.length180
  • s1.man.weigth78
  • / struct record b2 10000, 7, 190, 78

11
STRUCTURES AS FUNCTION ARGUMENTS 1
  • struct date int day int month int year
  • struct date calculate ( struct date d)
  • main()
  • struct date today 12, 10, 2005
  • struct date next_day
  • next_day calculate(today)
  • printf(Next day is d.d.d,
    next_day.day,next_day.month,next_day.year)
  • struct date calculate ( struct date d)
  • struct day n
  • if( d.day30)
  • if(d.month12) n.day1 n.month1
    n.yeard.year1
  • else n.day1n.monthd.month1
    n.yeard.year
  • else n.dayd.day1 n.monthd.month
    n.yeard.year
  • return n

12
STRUCTURES AS FUNCTION ARGUMENTS 2
  • includeltstdio.hgt
  • struct student char last_name int st_id int
    grade
  • int average ( struct student x, int size)
  • main()
  • typedef struct student st
  • st class30 int av,i
  • for(i0ilt30i)
  • scanf(d, classi.grade)
  • avaverage(class,30) // average(class0,30)
  • int average ( struct student x, int size) //
    int average ( struct student x, int size)
  • // int average ( st x, int size)
  • int a0 int i
  • for(i0iltsizei)

13
STRUCTURES AS FUNCTION ARGUMENTS 3
  • includeltstdio.hgt
  • struct complex double re double im
  • void add_on( struct complex a , struct complex
    b) /it will add these two complex
  • numbers and assign the result to a /
  • main()
  • struct complex x,y
  • x.re2 y.re4
  • x.im3 y.im6
  • printf(x is d di\n, x.re, x.im)
  • printf(y is d di\n, y.re, y.im)
  • add_on(x,y)
  • printf(x is d di\n, x.re, x.im)
  • printf(y is d di\n, y.re, y.im)
  • void add_on( struct complex a , struct complex
    b)
  • (a).re (a).re (b).re // a-gtre a-gtre
    b-gtre
  • (a).im (a).im (b).im //a-gtim a-gtim
    b-gtim

14
ACCESSING MEMBERS OF A STRUCTURE
  • struct student
  • char last_name int st_id char grade
  • main()
  • struct student tmp, ptmp
  • tmp.gradeA tmp.last_nameCasper
    tmp.st_id1000
  • printf(d\n,tmp.grade)
  • printf(d\n,p-gtgrade)
  • printf(s\n,tmp.last_name)
  • printf(s\n,p-gtlast_name)
  • printf(s\n,(p).last_name)
  • printf(c\n,(p-gtlast_name)2)
  • printf(c\n,((p-gtlast_name2))
  • printf(c\n,p-gtlast_name1) // -gt
    operator has a higher level of precedence
  • printf(c\n,((p-gtlast_name))1)

15
STACKS
  • define STACK_SIZE 4
  • typedef struct st
  • int data STACK_SIZE int top stack
  • void reset(stack stk)
  • stk-gttop-1
  • void push( int c, stack stk)
  • if( stk-gttop! STACK_SIZE-1)
  • stk-gttop stk-gtdatastk-gttopc
  • else printf(Stack is full!!\n)
  • int pop (stack stk)
  • if(stk-gttop!-1)

main() int k 1,2,3,4,5 stack n int
x reset(n) push(4,n) push(5,n) pus
h(3,n) push(2,n) push(1,n) x
pop(n) push(11,n) x pop(n) x
pop(n) x pop(n) push(3,n) push(2,n) x
pop(n)
16
UNIONS
  • includeltstdio.hgt
  • union int_or_char
  • char c
  • int i
  • main()
  • union int_or_char test
  • test.i 83
  • printf("i d\n", test)
  • printf("c c\n",test)
  • test.c 'A'
  • printf("i d\n",test)
  • printf("c c\n", test)
Write a Comment
User Comments (0)
About PowerShow.com