8 Hello!! C
2
3
4
struct - {...... }; struct jum{ int x_axis; int y_axis; }; struct - {...... } - ; struct jum{ int x_axis; int y_axis; }point1, *point2; 5
struct {....... } - ; struct{ int x_axis; int y_axis; }point1, *point2; struct - - ; struct jum point1, *point2; 6
Case I. struct number{ doouble real; double imaginary; }; struct number x1; x1.real = 2.0; x1.imaginary = 3.0; 7
[ 8-1] #include <stdio.h> main(){ struct people{ char name; int height; int weight; } data_1,data_2; data_1.name='a'; data_1.height=170; data_1.weight=70; data_2.name='b'; data_2.height=165; data_2.weight=55; printf("data_1.name=%c data_1.height=%d data_1.weight=%d\n", data_1.name,data_1.height,data_1.weight) ; 8
[ 8-1] printf("data_2.name=%c data_2.height=%d data_1.weight=%d\n", data_2.name,data_2.height,data_2.weight) ; } [ ] data_1.name=a data_1.height=170 data_1.weight=70 data_2.name=b data_2.height=165 data_2.weight=55 9
[ 8-2] #include <stdio.h> struct profile { char name[20]; char address[20]; char telephone[20]; char birthday[20]; } data={"han","seoul","361-2528","75/03/10" }; main(){ printf("name address telephone birthday\n"); printf("%s %s %s %s\n", data.name,data.address,data.telephone,data.birthday); } [ ] name address telephone birthday HAN SEOUL 361-2528 75/03/10 10
[ 8-3] #include <stdio.h> struct profile { char name[20]; char address[20]; char telephone[20]; char birthday[20]; } ; main(){ struct profile data={"han","seoul","361-2528","75/03/10" }; printf("name address telephone birthday\n"); printf(" %s %s %s %s\n", data.name,data.address,data.telephone,data.birthday); } [ ] name address telephone birthday HAN SEOUL 361-2528 75/03/10 11
[ 8-4] #include <stdio.h> #include <string.h> struct profile { char name[20]; char address[20]; char telephone[20]; char birthday[20]; } ; 12
[ 8-4] main(){ struct profile data; strcpy(data.name,"han"); strcpy(data.address,"seoul"); strcpy(data.telephone,"361-2528"); strcpy(data.birthday,"75/03/10"); printf("name address telephone birthday\n"); printf("%s %s %s %s\n", data.name,data.address,data.telephone,data.birthday); } 13
Case II -> struct number{ doouble real; double imaginary; }; struct number *x2; (*x2).real x2->real (*x2).imaginary x2->imaginary 14
[ 8-5] #include<stdio.h> #include<string.h> [ ] main(){ Product name is pencil struct product { Color is white,and cost is $3 char name[20]; char color[20]; int cost; } a,*pnt ; pnt=&a; strcpy(pnt->name,"pencil"); strcpy(pnt->color,"white"); pnt->cost = 3; printf("product name is %s\n",pnt->name); printf("color is %s,and cost is $%d\n", pnt->color,pnt->cost); } 15
[ 8-6] #include<stdio.h> struct hello{ char *pnt1; char *pnt2; char *pnt3; } greeting ; main(){ int x; printf("input'1'if morning,'2'if afternoon,'3'if evening \n"); scanf("%d",&x); greeting.pnt1 = "Good morning"; greeting.pnt2 = "Good afternoon"; greeting.pnt3 = "Good evening"; 16
[ 8-6] if(x==1) printf("%s",greeting.pnt1); else if(x==2) printf("%s",greeting.pnt2); else if(x==3) printf("%s",greeting.pnt3); else printf("error"); } [ ] Input'1'if morning,'2'if afternoon,'3'if evening 1 Good morning 17
[ 8-7] #include <stdio.h> main() { struct number{ int x; int y; }; struct number a[5], *pnt; pnt = a; int i; for(i=0 ;i<5 ;i++) printf("pnt+%d 's address is %p and it points to %p\n", i,&pnt+i,pnt+i); } 18
[ 8-7] [ ] pnt+0 's address is 1669:0FFC and it points to 1669:0FE8 pnt+1 's address is 1669:1000 and it points to 1669:0FEC pnt+2 's address is 1669:1004 and it points to 1669:0FF0 pnt+3 's address is 1669:1008 and it points to 1669:0FF4 pnt+4 's address is 1669:100C and it points to 1669:0FF8 19
:. struct jum{ int x_axis; int y_axis; }; /* */ struct rectangle{ struct jum point1; struct jum point2; }; 20
[ 8-8] #include<stdio.h> struct dot { int x; int y; }; struct square { struct dot topleft; struct dot bottomrgt; } rectangle; long area; main(){ printf("input the x coordinate of top left: "); scanf("%d",&rectangle.topleft.x); printf("input the y coordinate of top left: "); 21
[ 8-8] scanf("%d",&rectangle.topleft.y); printf("input the x coordinate of bottom right: "); scanf("%d",&rectangle.bottomrgt.x); printf("input the y coordinate of bottom right: "); scanf("%d",&rectangle.bottomrgt.y); area=(rectangle.bottomrgt.x - rectangle.topleft.x) * (rectangle.topleft.y - rectangle.bottomrgt.y); printf("the area of the rectangular form is %ld\n",area); } [ ] Input the x coordinate of top left: 2 Input the y coordinate of top left: 6 Input the y coordinate of bottom right: 8 Input the y coordinate of bottom right: 3 The area of the rectangular form is 18 22
[ 8-9] #include<stdio.h> main(){ struct dot{ int x; int y; struct dot *pnt; } dot1 ; dot1.pnt = & dot1 ; printf("address of dot1 is %p\n",&dot1); printf("dot1.pnt points to itself (%p)\n",dot1.pnt); printf("address of dot1.pnt is %p\n",&dot1.pnt); } [ ] address of dot1 is 166D:0FF8 dot1.pnt points to itself (166D:0FF8) address of dot1.pnt is 166D:0FFC 23
. struct s_tag{ union u_tag{ int x; int x; int y; int y; char *p; char *p; s_val; u_val; }; }; 24
u_val.p u_val.x s_val.x u_val.y. s_val.p s_val.y. 25
[ 8-10] /* */ #include <stdio.h> union number{ int x; int y; } ex1; struct num{ int x; int y; } ex2; main(){ ex1.x = 1234; ex1.y = 4321; 26
[ 8-10] printf("union member variable = %d\n", ex1.x); ex2.x = 1234; ex2.y = 4321; printf("struct member variable = %d\n",ex2.x); } [ ] union member variable = 4321 struct member variable = 1234 27
struct person{ char name[20]; char sex; int age; }; /* p */ /* p */ void display1(struct person p) { printf("%s : %c : %d \n", p.name, p.sex, p.age); } 28
[ 8-11] #include <stdio.h> struct person{ char name[20]; char sex; int age; }; void display(struct person p) /* person p */ { printf("%s : %c : %d \n", p.name, p.sex, p.age); } 29
[ 8-11] main() { struct person people={ "Chan Ho Park", 'm', 26 }; } printf("\n name sex age \n"); display(people); [ ] name sex age Chan Ho Park : m : 26 30
[ 8-12] #include <stdio.h> [ ] struct person{ name sex age char name[20]; Chan Ho Park : m : 26 char sex; int age; }; void display(struct person *p){ printf("%s : %c : %d \n", p->name, p->sex, p->age); } main(){ struct person people={ "Chan Ho Park", 'm', 26 }; printf("\n name sex age \n"); display(&people); } 31
[ 8-13] #include<stdio.h> struct marks { int math; int phys; int biol; } temp = {0,0,0}; marks funct(int math, int phys,int biol); void main(void){ struct marks point ; int a,b,c,i,t; printf("input total number of tests : "); scanf("%d",&t); for(i=0;i<t;i++) { printf("input marks in math, %d th test : ",i+1); scanf("%d",&a); 32
[ 8-13] printf("input marks in physics, %d th test : ",i+1); scanf("%d",&b); printf("input marks in biology, %d th test : ",i+1); scanf("%d",&c); point=funct(a,b,c); /* */ } printf("*****************************************\n"); printf("average marks in math : %d\n",point.math/t); printf("average marks in physics : %d\n",point.phys/t); printf("average marks in biology : %d\n",point.biol/t); } marks funct(int math, int phys,int biol) { temp.math = math + temp.math; temp.phys = phys + temp.phys; temp.biol = biol + temp.biol; return temp; } 33
[ 8-13] [ ] Input total number of tests : 2 Input marks in math, 1 th test : 60 Input marks in physics, 1 th test : 70 Input marks in biology, 1 th test : 90 Input marks in math, 2 th test : 50 Input marks in physics, 2 th test : 80 Input marks in biology, 2 th test : 100 ***************************************** average marks in math : 55 average marks in physics : 75 average marks in biology : 95 34
( linear linked list ) ( binary tree structure ) 35
( linear linked list ) :, struct linked_list{ char *p; struct linked_list *next; }; head struct linked_list{ char ch; struct linked_list *backward; struct linked_list *forward; }; p p p p 0 head 0 0 36
( binary tree structure ) struct binary{ char *p; struct binary *left; struct binary *right; }; p left child right child p p p p p p 37
struct node{ char *name; char sex; int age; struct node *next; }; struct node *head; struct node *p; for(p=head; p->next!=null; ){ p=p->next; p->next=&d; d.next = NULL; } 38
p->next = q->next; q->next = p; q ( ) q->next q ( ) q->next->next p 39
p->next = p->next->next; p p->next->next p p->next 40
int struct - { : ;..... : ; }; struct int_num{ unsigned field1: 2; unsigned fiedl2: 4; unsigned field3: 8; }num; 41
int unsigned 0 16 ".(dot) 42
han.code MSB LSB 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 han.final han.medial han.initial han.flag on :, off : 43