SlideShare a Scribd company logo
Structure
2
Declaring Structures
 Difference between Array and structure
– Array
• All the element must be the same type
• Access each element by index
– Structure
• It can consist of different type elements
• Each element has a name
• Access each element by name
3
Declaring Structures
 struct declaration
– Collection of members (elements)
[Ex] struct student { /* structure consists of 2 elements */
int std_id;
char name [20] ;
} ;
4
Declaring Structures
 struct declaration
- struct tag: Name of structure
- You can declare variables in the structure type
[Ex] struct student {
int std_id;
char name [20] ;
};
[Ex] struct student p1, p2 ; /* correct declaration */
student p1, p2; /* wrong declaration */
struct tag:
It can be omitted.
5
Accessing a Member
 Struct member operator ‘.’
– Access for a member of structure using ‘.’
struct student {
int std_id;
char name[20];
} ;
void main() {
struct student p ;
p.std_id = 1 ;
strcpy( p.name, “Monica” ) ;
}
1
Monica
p
std_id
name
6
Accessing a Member
 Member operation
struct student {
int std_id;
char name[20];
} ;
void main() {
struct student p ;
scanf(“%s”, p.name);
p.std_id = 258; /* assignment */
p.std_id++; /* increment */
}
typedef of struct
 Example
7
struct student {
char name[20] ;
int id ;
} ;
void main() {
struct student std1 ;
struct student std2 ;
…
}
struct student {
char name[20] ;
int id ;
} ;
typedef struct student Student ;
void main() {
Student std1 ;
Student std2 ;
…
}
typedef of struct
 Example
8
typedef struct student {
char name[20] ;
int id ;
} Student;
struct student {
char name[20] ;
int id ;
} ;
typedef struct student Student ;
void main()
{
Student p ;
scanf( “%d”, &p.std_id ) ;
scanf( “%s”, p.name ) ;
printf( “%d ”, p.std_id ) ;
printf( “%sn”, p.name ) ;
}
9
Initializing Structures
 Example
typedef struct student {
int std_id;
char name [20] ;
} Student ;
void main() {
Student person1 = {1, “Gil Dong”} ;
Student person2 = { 2, “Sun Shin” } ;
}
person1
person1.number person1.name
1 Gil Dong person2
person2.number person2.name
2 Sun Shin
10
Structure Pointer
 Pointer Declaration
typedef struct student {
int std_id;
char name[20];
} Student;
void main() {
Student s={1, “Gil Dong”};
Student *p = &s ;
}
s
1 Gil Dong
p
11
Structure Pointer
 Member access through pointer
typedef struct student {
int std_id;
char name[20];
} Student;
Student s, *p = &s ;
(*p).std_id = 10 ;
strcpy( (*p).name, “Sun Shin” ) ;
Student s, *p = &s ;
p->std_id = 10 ;
strcpy( p->name, “Sun Shin” ) ;
12
Structure Pointer
typedef struct shape {
int x, y ;
char name[10] ;
} Shape;
void main() {
Shape s, *p = &s;
scanf (“%d %d %s”, &p->x , &p->y, p->name ) ;
p->x *= 2;
p->y %= 5;
printf (“%d %d %sn”, p->x , p->y, p->name ) ;
}
13
Structure Pointer
typedef struct shape {
int x, y ;
char name[10] ;
} Shape;
void main() {
Shape s, *p = &s;
scanf (“%d %d %s”, &(*p).x , &(*p).y, (*p).name ) ;
(*p).x *= 2;
(*p).y %= 5;
printf (“%d %d %sn”, (*p).x , (*p). y, (*p). name ) ;
}
14
Precedence and Associativity of Operators
( ) [ ] . -> ++(postfix) --(postfix) left to right
++(prefix) --(prefix) ! ~ sizeof(type)
+(unary) -(unary) &(address) *(dereference)
right to left
* / % left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
&& left to right
|| left to right
? : right to left
= += -= *= /= %= >>= <<= &= ^= |= right to left
15
Functions and Assignment
 Assignment into struct variables
typedef struct student {
int std_id;
char name[20];
} Student;
void main() {
Student p1 = { 1, “Gil Dong”}, p2 ;
p2 = p1 ; // OK???
}
1
Gil Dong
p1
?
?
p2
16
Functions and Assignment
 Assignment into struct variables
typedef struct student {
int std_id;
char name[20];
} Student;
void main() {
Student p1 = { 1, “Gil Dong”}, p2 ;
copy_student( p1, p2 ) ;
}
void copy_student( Student p1,
Student p2 )
{
p1.std_id = p2.std_id ;
strcpy( p1.name, p2.name ) ;
}
Are the values of p2 in main() changed or not?
17
Functions and Assignment
 Assignment into struct variables
typedef struct student {
int std_id;
char name[20];
} Student;
void main() {
Student p1 = { 1, “Gil Dong”}, p2 ;
copy_student( &p1, &p2 ) ;
}
void copy_student( Student *p1,
Student *p2 )
{
p1->std_id = p2->std_id ;
strcpy( p1->name, p2->name ) ;
}
Are the values of p2 in main() changed or not?
18
Arrays of Structure
 Definition
typedef struct student {
int std_id;
char name[20];
} Student;
Student my_student[100] ;
my_student[0]
1 Gil Dong
my_student[1]
5 Sun Shin ………
19
Initialization of Structures
 Initializing an Array of Structures
Student my_student[20] =
{ {1, “Gil Dong”},
{2, “Sun Sin”},
{3, “Kuk Jung”},
:
} ;

More Related Content

Similar to 12 2. structure

Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classesAlisha Korpal
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
YOGESH SINGH
 
Structure & union
Structure & unionStructure & union
Structure & union
Rupesh Mishra
 
Structures
StructuresStructures
Structures
DrJasmineBeulahG
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptx
Boni Yeamin
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
Gem WeBlog
 
Clang2018 class5
Clang2018 class5Clang2018 class5
Clang2018 class5
tagawakiyoshi
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
BArulmozhi
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
Structures in c
Structures in cStructures in c
Structures in c
kalavathisugan
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
sumitbardhan
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEm
home
 
การใช้ Turbo C ชุดที่ 12 structure
การใช้ Turbo C ชุดที่ 12 structureการใช้ Turbo C ชุดที่ 12 structure
การใช้ Turbo C ชุดที่ 12 structure
Know Mastikate
 
C++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsC++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for Students
MuhammadAli224595
 
L10
L10L10
L10
lksoo
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 

Similar to 12 2. structure (20)

13. structure
13. structure13. structure
13. structure
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structures
StructuresStructures
Structures
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptx
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Clang2018 class5
Clang2018 class5Clang2018 class5
Clang2018 class5
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Structures in c
Structures in cStructures in c
Structures in c
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEm
 
การใช้ Turbo C ชุดที่ 12 structure
การใช้ Turbo C ชุดที่ 12 structureการใช้ Turbo C ชุดที่ 12 structure
การใช้ Turbo C ชุดที่ 12 structure
 
C++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsC++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for Students
 
L10
L10L10
L10
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 

More from 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 

More from 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 

Recently uploaded

Do Linguistics Still Matter in the Age of Large Language Models.pptx
Do Linguistics Still Matter in the Age of Large Language Models.pptxDo Linguistics Still Matter in the Age of Large Language Models.pptx
Do Linguistics Still Matter in the Age of Large Language Models.pptx
Slator- Language Industry Intelligence
 
role of women and girls in various terror groups
role of women and girls in various terror groupsrole of women and girls in various terror groups
role of women and girls in various terror groups
sadiakorobi2
 
31052024_First India Newspaper Jaipur.pdf
31052024_First India Newspaper Jaipur.pdf31052024_First India Newspaper Jaipur.pdf
31052024_First India Newspaper Jaipur.pdf
FIRST INDIA
 
Preview of Court Document for Iseyin community
Preview of Court Document for Iseyin communityPreview of Court Document for Iseyin community
Preview of Court Document for Iseyin community
contact193699
 
Sharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdf
Sharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdfSharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdf
Sharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdf
bhavenpr
 
03062024_First India Newspaper Jaipur.pdf
03062024_First India Newspaper Jaipur.pdf03062024_First India Newspaper Jaipur.pdf
03062024_First India Newspaper Jaipur.pdf
FIRST INDIA
 
2024 is the point of certainty. Forecast of UIF experts
2024 is the point of certainty. Forecast of UIF experts2024 is the point of certainty. Forecast of UIF experts
2024 is the point of certainty. Forecast of UIF experts
olaola5673
 
Draft-1-Resolutions-Key-Interventions-.pdf
Draft-1-Resolutions-Key-Interventions-.pdfDraft-1-Resolutions-Key-Interventions-.pdf
Draft-1-Resolutions-Key-Interventions-.pdf
bhavenpr
 
01062024_First India Newspaper Jaipur.pdf
01062024_First India Newspaper Jaipur.pdf01062024_First India Newspaper Jaipur.pdf
01062024_First India Newspaper Jaipur.pdf
FIRST INDIA
 
Hogan Comes Home: an MIA WWII crewman is returned
Hogan Comes Home: an MIA WWII crewman is returnedHogan Comes Home: an MIA WWII crewman is returned
Hogan Comes Home: an MIA WWII crewman is returned
rbakerj2
 
Codes n Conventionss copy (1).paaaaaaptx
Codes n Conventionss copy (1).paaaaaaptxCodes n Conventionss copy (1).paaaaaaptx
Codes n Conventionss copy (1).paaaaaaptx
ZackSpencer3
 
Resolutions-Key-Interventions-28-May-2024.pdf
Resolutions-Key-Interventions-28-May-2024.pdfResolutions-Key-Interventions-28-May-2024.pdf
Resolutions-Key-Interventions-28-May-2024.pdf
bhavenpr
 
AI and Covert Influence Operations: Latest Trends
AI and Covert Influence Operations: Latest TrendsAI and Covert Influence Operations: Latest Trends
AI and Covert Influence Operations: Latest Trends
CI kumparan
 

Recently uploaded (13)

Do Linguistics Still Matter in the Age of Large Language Models.pptx
Do Linguistics Still Matter in the Age of Large Language Models.pptxDo Linguistics Still Matter in the Age of Large Language Models.pptx
Do Linguistics Still Matter in the Age of Large Language Models.pptx
 
role of women and girls in various terror groups
role of women and girls in various terror groupsrole of women and girls in various terror groups
role of women and girls in various terror groups
 
31052024_First India Newspaper Jaipur.pdf
31052024_First India Newspaper Jaipur.pdf31052024_First India Newspaper Jaipur.pdf
31052024_First India Newspaper Jaipur.pdf
 
Preview of Court Document for Iseyin community
Preview of Court Document for Iseyin communityPreview of Court Document for Iseyin community
Preview of Court Document for Iseyin community
 
Sharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdf
Sharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdfSharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdf
Sharjeel-Imam-Judgement-CRLA-215-2024_29-05-2024.pdf
 
03062024_First India Newspaper Jaipur.pdf
03062024_First India Newspaper Jaipur.pdf03062024_First India Newspaper Jaipur.pdf
03062024_First India Newspaper Jaipur.pdf
 
2024 is the point of certainty. Forecast of UIF experts
2024 is the point of certainty. Forecast of UIF experts2024 is the point of certainty. Forecast of UIF experts
2024 is the point of certainty. Forecast of UIF experts
 
Draft-1-Resolutions-Key-Interventions-.pdf
Draft-1-Resolutions-Key-Interventions-.pdfDraft-1-Resolutions-Key-Interventions-.pdf
Draft-1-Resolutions-Key-Interventions-.pdf
 
01062024_First India Newspaper Jaipur.pdf
01062024_First India Newspaper Jaipur.pdf01062024_First India Newspaper Jaipur.pdf
01062024_First India Newspaper Jaipur.pdf
 
Hogan Comes Home: an MIA WWII crewman is returned
Hogan Comes Home: an MIA WWII crewman is returnedHogan Comes Home: an MIA WWII crewman is returned
Hogan Comes Home: an MIA WWII crewman is returned
 
Codes n Conventionss copy (1).paaaaaaptx
Codes n Conventionss copy (1).paaaaaaptxCodes n Conventionss copy (1).paaaaaaptx
Codes n Conventionss copy (1).paaaaaaptx
 
Resolutions-Key-Interventions-28-May-2024.pdf
Resolutions-Key-Interventions-28-May-2024.pdfResolutions-Key-Interventions-28-May-2024.pdf
Resolutions-Key-Interventions-28-May-2024.pdf
 
AI and Covert Influence Operations: Latest Trends
AI and Covert Influence Operations: Latest TrendsAI and Covert Influence Operations: Latest Trends
AI and Covert Influence Operations: Latest Trends
 

12 2. structure

  • 2. 2 Declaring Structures  Difference between Array and structure – Array • All the element must be the same type • Access each element by index – Structure • It can consist of different type elements • Each element has a name • Access each element by name
  • 3. 3 Declaring Structures  struct declaration – Collection of members (elements) [Ex] struct student { /* structure consists of 2 elements */ int std_id; char name [20] ; } ;
  • 4. 4 Declaring Structures  struct declaration - struct tag: Name of structure - You can declare variables in the structure type [Ex] struct student { int std_id; char name [20] ; }; [Ex] struct student p1, p2 ; /* correct declaration */ student p1, p2; /* wrong declaration */ struct tag: It can be omitted.
  • 5. 5 Accessing a Member  Struct member operator ‘.’ – Access for a member of structure using ‘.’ struct student { int std_id; char name[20]; } ; void main() { struct student p ; p.std_id = 1 ; strcpy( p.name, “Monica” ) ; } 1 Monica p std_id name
  • 6. 6 Accessing a Member  Member operation struct student { int std_id; char name[20]; } ; void main() { struct student p ; scanf(“%s”, p.name); p.std_id = 258; /* assignment */ p.std_id++; /* increment */ }
  • 7. typedef of struct  Example 7 struct student { char name[20] ; int id ; } ; void main() { struct student std1 ; struct student std2 ; … } struct student { char name[20] ; int id ; } ; typedef struct student Student ; void main() { Student std1 ; Student std2 ; … }
  • 8. typedef of struct  Example 8 typedef struct student { char name[20] ; int id ; } Student; struct student { char name[20] ; int id ; } ; typedef struct student Student ; void main() { Student p ; scanf( “%d”, &p.std_id ) ; scanf( “%s”, p.name ) ; printf( “%d ”, p.std_id ) ; printf( “%sn”, p.name ) ; }
  • 9. 9 Initializing Structures  Example typedef struct student { int std_id; char name [20] ; } Student ; void main() { Student person1 = {1, “Gil Dong”} ; Student person2 = { 2, “Sun Shin” } ; } person1 person1.number person1.name 1 Gil Dong person2 person2.number person2.name 2 Sun Shin
  • 10. 10 Structure Pointer  Pointer Declaration typedef struct student { int std_id; char name[20]; } Student; void main() { Student s={1, “Gil Dong”}; Student *p = &s ; } s 1 Gil Dong p
  • 11. 11 Structure Pointer  Member access through pointer typedef struct student { int std_id; char name[20]; } Student; Student s, *p = &s ; (*p).std_id = 10 ; strcpy( (*p).name, “Sun Shin” ) ; Student s, *p = &s ; p->std_id = 10 ; strcpy( p->name, “Sun Shin” ) ;
  • 12. 12 Structure Pointer typedef struct shape { int x, y ; char name[10] ; } Shape; void main() { Shape s, *p = &s; scanf (“%d %d %s”, &p->x , &p->y, p->name ) ; p->x *= 2; p->y %= 5; printf (“%d %d %sn”, p->x , p->y, p->name ) ; }
  • 13. 13 Structure Pointer typedef struct shape { int x, y ; char name[10] ; } Shape; void main() { Shape s, *p = &s; scanf (“%d %d %s”, &(*p).x , &(*p).y, (*p).name ) ; (*p).x *= 2; (*p).y %= 5; printf (“%d %d %sn”, (*p).x , (*p). y, (*p). name ) ; }
  • 14. 14 Precedence and Associativity of Operators ( ) [ ] . -> ++(postfix) --(postfix) left to right ++(prefix) --(prefix) ! ~ sizeof(type) +(unary) -(unary) &(address) *(dereference) right to left * / % left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right && left to right || left to right ? : right to left = += -= *= /= %= >>= <<= &= ^= |= right to left
  • 15. 15 Functions and Assignment  Assignment into struct variables typedef struct student { int std_id; char name[20]; } Student; void main() { Student p1 = { 1, “Gil Dong”}, p2 ; p2 = p1 ; // OK??? } 1 Gil Dong p1 ? ? p2
  • 16. 16 Functions and Assignment  Assignment into struct variables typedef struct student { int std_id; char name[20]; } Student; void main() { Student p1 = { 1, “Gil Dong”}, p2 ; copy_student( p1, p2 ) ; } void copy_student( Student p1, Student p2 ) { p1.std_id = p2.std_id ; strcpy( p1.name, p2.name ) ; } Are the values of p2 in main() changed or not?
  • 17. 17 Functions and Assignment  Assignment into struct variables typedef struct student { int std_id; char name[20]; } Student; void main() { Student p1 = { 1, “Gil Dong”}, p2 ; copy_student( &p1, &p2 ) ; } void copy_student( Student *p1, Student *p2 ) { p1->std_id = p2->std_id ; strcpy( p1->name, p2->name ) ; } Are the values of p2 in main() changed or not?
  • 18. 18 Arrays of Structure  Definition typedef struct student { int std_id; char name[20]; } Student; Student my_student[100] ; my_student[0] 1 Gil Dong my_student[1] 5 Sun Shin ………
  • 19. 19 Initialization of Structures  Initializing an Array of Structures Student my_student[20] = { {1, “Gil Dong”}, {2, “Sun Sin”}, {3, “Kuk Jung”}, : } ;