SlideShare a Scribd company logo
1 of 19
Download to read offline
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 Unit4YOGESH SINGH
 
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.pptxBoni Yeamin
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.pptBArulmozhi
 
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.pdfsudhakargeruganti
 
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-7sumitbardhan
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmhome
 
การใช้ Turbo C ชุดที่ 12 structure
การใช้ Turbo C ชุดที่ 12 structureการใช้ Turbo C ชุดที่ 12 structure
การใช้ Turbo C ชุดที่ 12 structureKnow 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 StudentsMuhammadAli224595
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta 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

Dominican American Coalition PAC Executive Summary
Dominican American Coalition PAC Executive SummaryDominican American Coalition PAC Executive Summary
Dominican American Coalition PAC Executive SummaryRDE GROUP CORP
 
Have A Complimentary Cheat Sheet, On Us!!!
Have A Complimentary Cheat Sheet, On Us!!!Have A Complimentary Cheat Sheet, On Us!!!
Have A Complimentary Cheat Sheet, On Us!!!Abdul-Hakim Shabazz
 
israeil_bnetaniahou_panel_report_eng.pdf
israeil_bnetaniahou_panel_report_eng.pdfisraeil_bnetaniahou_panel_report_eng.pdf
israeil_bnetaniahou_panel_report_eng.pdfssuser5750e1
 
Encore portal Project PPt on live portal
Encore portal Project PPt on live portalEncore portal Project PPt on live portal
Encore portal Project PPt on live portalEshantRawat2
 
PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...
PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...
PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...Faga1939
 
CaseThe legal victory in the turmeric patent case safeguarded traditional kno...
CaseThe legal victory in the turmeric patent case safeguarded traditional kno...CaseThe legal victory in the turmeric patent case safeguarded traditional kno...
CaseThe legal victory in the turmeric patent case safeguarded traditional kno...alpha012343210
 
Minnesota Timberwolves Bring Ya Ass T Shirt
Minnesota Timberwolves Bring Ya Ass T ShirtMinnesota Timberwolves Bring Ya Ass T Shirt
Minnesota Timberwolves Bring Ya Ass T Shirtniherranjansingha
 
Positive Effects Of Social Media On Mental Health
Positive Effects Of Social Media On Mental HealthPositive Effects Of Social Media On Mental Health
Positive Effects Of Social Media On Mental HealthTheUnitedIndian
 
Embed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;j
Embed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;jEmbed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;j
Embed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;jbhavenpr
 
May 2024 - Crypto Market Report_FINAL.pdf
May 2024 - Crypto Market Report_FINAL.pdfMay 2024 - Crypto Market Report_FINAL.pdf
May 2024 - Crypto Market Report_FINAL.pdfmanisha194592
 
21052024_First India Newspaper Jaipur.pdf
21052024_First India Newspaper Jaipur.pdf21052024_First India Newspaper Jaipur.pdf
21052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
World Politics as Black & White Iran and Israel or how people fall victims of...
World Politics as Black & White Iran and Israel or how people fall victims of...World Politics as Black & White Iran and Israel or how people fall victims of...
World Politics as Black & White Iran and Israel or how people fall victims of...Muhammad Shamsaddin Megalommatis
 
Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[
Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[
Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[bhavenpr
 
19052024_First India Newspaper Jaipur.pdf
19052024_First India Newspaper Jaipur.pdf19052024_First India Newspaper Jaipur.pdf
19052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
Embed-3-1-1.pdf The ECI direction on April 2, 2024 can be read here:
Embed-3-1-1.pdf  The ECI direction on April 2, 2024 can be read here:Embed-3-1-1.pdf  The ECI direction on April 2, 2024 can be read here:
Embed-3-1-1.pdf The ECI direction on April 2, 2024 can be read here:bhavenpr
 
R$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatórios
R$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatóriosR$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatórios
R$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatóriosMaurílio Júnior
 
Meta_AI_ads_investigation.pdfldoljjwejolejolol
Meta_AI_ads_investigation.pdfldoljjwejolejololMeta_AI_ads_investigation.pdfldoljjwejolejolol
Meta_AI_ads_investigation.pdfldoljjwejolejololbhavenpr
 

Recently uploaded (17)

Dominican American Coalition PAC Executive Summary
Dominican American Coalition PAC Executive SummaryDominican American Coalition PAC Executive Summary
Dominican American Coalition PAC Executive Summary
 
Have A Complimentary Cheat Sheet, On Us!!!
Have A Complimentary Cheat Sheet, On Us!!!Have A Complimentary Cheat Sheet, On Us!!!
Have A Complimentary Cheat Sheet, On Us!!!
 
israeil_bnetaniahou_panel_report_eng.pdf
israeil_bnetaniahou_panel_report_eng.pdfisraeil_bnetaniahou_panel_report_eng.pdf
israeil_bnetaniahou_panel_report_eng.pdf
 
Encore portal Project PPt on live portal
Encore portal Project PPt on live portalEncore portal Project PPt on live portal
Encore portal Project PPt on live portal
 
PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...
PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...
PEACE BETWEEN ISRAEL AND PALESTINE REQUIRES EXTREMISTS OUT OF POWER AND RESTR...
 
CaseThe legal victory in the turmeric patent case safeguarded traditional kno...
CaseThe legal victory in the turmeric patent case safeguarded traditional kno...CaseThe legal victory in the turmeric patent case safeguarded traditional kno...
CaseThe legal victory in the turmeric patent case safeguarded traditional kno...
 
Minnesota Timberwolves Bring Ya Ass T Shirt
Minnesota Timberwolves Bring Ya Ass T ShirtMinnesota Timberwolves Bring Ya Ass T Shirt
Minnesota Timberwolves Bring Ya Ass T Shirt
 
Positive Effects Of Social Media On Mental Health
Positive Effects Of Social Media On Mental HealthPositive Effects Of Social Media On Mental Health
Positive Effects Of Social Media On Mental Health
 
Embed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;j
Embed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;jEmbed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;j
Embed-4-1-1.pdf vm ;sdkp[kdp[kpdkpodp;p;j
 
May 2024 - Crypto Market Report_FINAL.pdf
May 2024 - Crypto Market Report_FINAL.pdfMay 2024 - Crypto Market Report_FINAL.pdf
May 2024 - Crypto Market Report_FINAL.pdf
 
21052024_First India Newspaper Jaipur.pdf
21052024_First India Newspaper Jaipur.pdf21052024_First India Newspaper Jaipur.pdf
21052024_First India Newspaper Jaipur.pdf
 
World Politics as Black & White Iran and Israel or how people fall victims of...
World Politics as Black & White Iran and Israel or how people fall victims of...World Politics as Black & White Iran and Israel or how people fall victims of...
World Politics as Black & White Iran and Israel or how people fall victims of...
 
Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[
Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[
Embed-4-3 (1).pdf cvxx'f['df[p'lf][l][fl][fl][][l[
 
19052024_First India Newspaper Jaipur.pdf
19052024_First India Newspaper Jaipur.pdf19052024_First India Newspaper Jaipur.pdf
19052024_First India Newspaper Jaipur.pdf
 
Embed-3-1-1.pdf The ECI direction on April 2, 2024 can be read here:
Embed-3-1-1.pdf  The ECI direction on April 2, 2024 can be read here:Embed-3-1-1.pdf  The ECI direction on April 2, 2024 can be read here:
Embed-3-1-1.pdf The ECI direction on April 2, 2024 can be read here:
 
R$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatórios
R$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatóriosR$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatórios
R$ 78 milhões: Estado aprova 593 propostas para acordos diretos de precatórios
 
Meta_AI_ads_investigation.pdfldoljjwejolejolol
Meta_AI_ads_investigation.pdfldoljjwejolejololMeta_AI_ads_investigation.pdfldoljjwejolejolol
Meta_AI_ads_investigation.pdfldoljjwejolejolol
 

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”}, : } ;