SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
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”},
:
} ;

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 웅식 전

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(학생)웅식 전
 

Mehr von 웅식 전 (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(학생)
 

Kürzlich hochgeladen

15042024_First India Newspaper Jaipur.pdf
15042024_First India Newspaper Jaipur.pdf15042024_First India Newspaper Jaipur.pdf
15042024_First India Newspaper Jaipur.pdfFIRST INDIA
 
Experience the Future of the Web3 Gaming Trend
Experience the Future of the Web3 Gaming TrendExperience the Future of the Web3 Gaming Trend
Experience the Future of the Web3 Gaming TrendFabwelt
 
16042024_First India Newspaper Jaipur.pdf
16042024_First India Newspaper Jaipur.pdf16042024_First India Newspaper Jaipur.pdf
16042024_First India Newspaper Jaipur.pdfFIRST INDIA
 
complaint-ECI-PM-media-1-Chandru.pdfra;;prfk
complaint-ECI-PM-media-1-Chandru.pdfra;;prfkcomplaint-ECI-PM-media-1-Chandru.pdfra;;prfk
complaint-ECI-PM-media-1-Chandru.pdfra;;prfkbhavenpr
 
Rohan Jaitley: Central Gov't Standing Counsel for Justice
Rohan Jaitley: Central Gov't Standing Counsel for JusticeRohan Jaitley: Central Gov't Standing Counsel for Justice
Rohan Jaitley: Central Gov't Standing Counsel for JusticeAbdulGhani778830
 
57 Bidens Annihilation Nation Policy.pdf
57 Bidens Annihilation Nation Policy.pdf57 Bidens Annihilation Nation Policy.pdf
57 Bidens Annihilation Nation Policy.pdfGerald Furnkranz
 
IndiaWest: Your Trusted Source for Today's Global News
IndiaWest: Your Trusted Source for Today's Global NewsIndiaWest: Your Trusted Source for Today's Global News
IndiaWest: Your Trusted Source for Today's Global NewsIndiaWest2
 
Global Terrorism and its types and prevention ppt.
Global Terrorism and its types and prevention ppt.Global Terrorism and its types and prevention ppt.
Global Terrorism and its types and prevention ppt.NaveedKhaskheli1
 

Kürzlich hochgeladen (8)

15042024_First India Newspaper Jaipur.pdf
15042024_First India Newspaper Jaipur.pdf15042024_First India Newspaper Jaipur.pdf
15042024_First India Newspaper Jaipur.pdf
 
Experience the Future of the Web3 Gaming Trend
Experience the Future of the Web3 Gaming TrendExperience the Future of the Web3 Gaming Trend
Experience the Future of the Web3 Gaming Trend
 
16042024_First India Newspaper Jaipur.pdf
16042024_First India Newspaper Jaipur.pdf16042024_First India Newspaper Jaipur.pdf
16042024_First India Newspaper Jaipur.pdf
 
complaint-ECI-PM-media-1-Chandru.pdfra;;prfk
complaint-ECI-PM-media-1-Chandru.pdfra;;prfkcomplaint-ECI-PM-media-1-Chandru.pdfra;;prfk
complaint-ECI-PM-media-1-Chandru.pdfra;;prfk
 
Rohan Jaitley: Central Gov't Standing Counsel for Justice
Rohan Jaitley: Central Gov't Standing Counsel for JusticeRohan Jaitley: Central Gov't Standing Counsel for Justice
Rohan Jaitley: Central Gov't Standing Counsel for Justice
 
57 Bidens Annihilation Nation Policy.pdf
57 Bidens Annihilation Nation Policy.pdf57 Bidens Annihilation Nation Policy.pdf
57 Bidens Annihilation Nation Policy.pdf
 
IndiaWest: Your Trusted Source for Today's Global News
IndiaWest: Your Trusted Source for Today's Global NewsIndiaWest: Your Trusted Source for Today's Global News
IndiaWest: Your Trusted Source for Today's Global News
 
Global Terrorism and its types and prevention ppt.
Global Terrorism and its types and prevention ppt.Global Terrorism and its types and prevention ppt.
Global Terrorism and its types and prevention ppt.
 

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