SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
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”},
:
} ;

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Plus de 웅식 전

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

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

Dernier

*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...anjanibaddipudi1
 
declarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdfdeclarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdfssuser5750e1
 
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreieGujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreiebhavenpr
 
04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdf04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
China's soft power in 21st century .pptx
China's soft power in 21st century   .pptxChina's soft power in 21st century   .pptx
China's soft power in 21st century .pptxYasinAhmad20
 
06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdf06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...Andy (Avraham) Blumenthal
 
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...Faga1939
 
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...IT Industry
 
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopkoEmbed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopkobhavenpr
 
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...hyt3577
 
05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdf05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
The political system of the united kingdom
The political system of the united kingdomThe political system of the united kingdom
The political system of the united kingdomlunadelior
 
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...srinuseo15
 
Politician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full DetailsPolitician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full DetailsVoterMood
 
Embed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdhEmbed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdhbhavenpr
 
Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024Insiger
 
Group_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the tradeGroup_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the tradeRahatulAshafeen
 
422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdf422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdflambardar420420
 

Dernier (20)

*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
 
declarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdfdeclarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdf
 
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreieGujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreie
 
04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdf04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdf
 
China's soft power in 21st century .pptx
China's soft power in 21st century   .pptxChina's soft power in 21st century   .pptx
China's soft power in 21st century .pptx
 
06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdf06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdf
 
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
 
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
 
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
 
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopkoEmbed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
 
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
 
05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdf05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdf
 
The political system of the united kingdom
The political system of the united kingdomThe political system of the united kingdom
The political system of the united kingdom
 
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
 
Politician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full DetailsPolitician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full Details
 
Embed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdhEmbed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdh
 
Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024
 
Group_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the tradeGroup_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the trade
 
9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR
9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR
9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR
 
422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdf422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdf
 

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