Title: STRUCTURES
1STRUCTURES
2Structures 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
3Structures
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
4Structures
struct record int ID char name char grade
struct record s1 struct record s2
s1
ID
name
grade
s2
ID
name
grade
5Structures
- 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
6Structures 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
7Structures 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 /
8Structures 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
9INITIALIZATION
- 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
10INITIALIZATION
- 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
11STRUCTURES 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
12STRUCTURES 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)
13STRUCTURES 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
14ACCESSING 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)
15STACKS
- 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)
16UNIONS
- 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)