SlideShare una empresa de Scribd logo
1 de 18
Descargar para leer sin conexión
Structure
2
Declaring Structures
§ Array와 structure의 차이점
– array
• Array의 모든 element는 같은 type이여야 한다.
• Index를 사용하여 각 element를 access한다.
– structure
• 다른 type의 element로 구성 될 수 있다.
• 각 element는 name을 갖는다.
• Name에 의해 각 element를 access한다.
3
Declaring Structures
§ struct declaration
– Collection of members(/elements)
[Ex] struct student { /* 2 elements로 구성된 structure */
int std_id;
char name [20] ;
} ;
int std_id, char name[20]
2 member를 포함하는 student라 불리우는 struct type을 정의
4
Declaring Structures
§ struct declaration
- struct tag: 정의 되는 structure를 지정하기 위한 name.
- 한 번 structure tag인 student가 정의되면, 이제 tag를 사용하
여 같은 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:
생략 가능하지만..
5
Accessing a Member
§ struct member operator ‘.’
– Structure의 각 member를 access하기 위해 ‘.’를 사용한다.
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student p ;
p.std_id = 1 ;
strcpy( p.name, “Monica” ) ;
1
Monica
p
std_id
name
6
Accessing a Member
§ member operation
[Ex] struct student {
int std_id;
char name[20];
}
struct student p ;
scanf(“%s”, p.name);
p.std_id = 258; /* assignment */
p.std_id++; /* increment */
7
Declaring Structures
§ Example
int main()
{
int std_id ;
char name[20] ;
scanf( “%d”, &std_id ) ;
scanf( “%s”, name ) ;
printf( “%d ”, std_id ) ;
scanf( “%sn”, name ) ;
return 0;
}
struct student {
int std_id;
char name [20] ;
} ;
int main()
{
struct student p ;
scanf( “%d”, &p.std_id ) ;
scanf( “%s”, p.name ) ;
printf( “%d ”, p.std_id ) ;
printf( “%sn”, p.name ) ;
return 0;
}
8
Declaring Structures
§ Initializing Struct variables
[Ex] struct student {
int std_id;
char name [20] ;
} ;
struct student person1 = { 1, “Gil Dong” } ;
struct student person2 = { 2, “Sun Shin” } ;
person1
person1.number person1.name
1 Gil Dong person2
person2.number person2.name
2 Sun Shin
9
Structure Pointer
§ Pointer 선언
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student s={1, “Gil Dong”}, *p ;
p = &s ;
s
1 Gil Dong
p
10
Structure Pointer
§ Pointer를 통한 member access
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student s, *p = &s ;
(*p).std_id = 10 ;
strcpy( (*p).name, “Sun Shin” ) ;
struct student s, *p = &s ;
p->std_id = 10 ;
strcpy( p->name, “Sun Shin” ) ;
11
Structure Pointer
[Ex] struct shape {
int x, y ;
char name[10] ;
};
struct 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 ) ;
12
Structure Pointer
[Ex] struct shape {
int x, y ;
char name[10] ;
};
struct 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
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
14
Functions and Assignment
§ struct 변수에 값 넣기
struct student {
int std_id;
char name[20];
} p1, p2;
struct student p1 = { 1, “Gil Dong”}, p2 ;
p2 = p1 ; // OK???
1
Gil Dong
p1
?
?
p2
OK. 원하는 대로 잘 됨
15
Functions and Assignment
§ struct 변수에 값 넣기
struct student {
int std_id;
char name[20];
} ;
int main() {
struct student p1 = { 1, “Gil Dong”}, p2 ;
copy_student( p1, p2 ) ;
return 0;
}
void copy_student( struct student p1,
struct student p2 )
{
p1.std_id = p2.std_id ;
strcpy( p1.name, p2.name ) ;
}
p2의 값은 어떻게 될까?
16
Functions and Assignment
§ struct 변수에 값 넣기
void copy_student( struct student *p1,
struct student *p2 )
{
p1->std_id = p2->std_id ;
strcpy( p1->name, p2->name ) ;
}
struct student {
int std_id;
char name[20];
} ;
int main() {
struct student p1 = { 1, “Gil Dong”}, p2 ;
copy_student( &p1, &p2 ) ;
return 0;
}
p2의 값은 어떻게 될까?
17
Arrays of Structure
§ Definition
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student my_student[100] ;
my_student[0]
1 Gil Dong
my_student[1]
5 Sun Shin ………
18
Initialization of Structures
§ Initializing an Array of Structures
[Ex] struct student {
int std_id ;
char name[20] ;
} ;
struct student my_student[20] =
{ {1, “Gil Dong”},
{2, “Sun Sin”},
{3, “Kuk Jung”},
:
} ;

Más contenido relacionado

La actualidad más candente

Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chanana
karan info
 
Crash Course to SQL in PHP
Crash Course to SQL in PHPCrash Course to SQL in PHP
Crash Course to SQL in PHP
webhostingguy
 

La actualidad más candente (14)

5. hello popescu
5. hello popescu5. hello popescu
5. hello popescu
 
11. delete record
11. delete record11. delete record
11. delete record
 
10. view one record
10. view one record10. view one record
10. view one record
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Object::Franger: Wear a Raincoat in your Code
Object::Franger: Wear a Raincoat in your CodeObject::Franger: Wear a Raincoat in your Code
Object::Franger: Wear a Raincoat in your Code
 
Command-Oriented Architecture
Command-Oriented ArchitectureCommand-Oriented Architecture
Command-Oriented Architecture
 
Smarty
SmartySmarty
Smarty
 
Writing testable js [by Ted Piotrowski]
Writing testable js [by Ted Piotrowski]Writing testable js [by Ted Piotrowski]
Writing testable js [by Ted Piotrowski]
 
Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chanana
 
Advanced theming
Advanced themingAdvanced theming
Advanced theming
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
Jquery plugin development
Jquery plugin developmentJquery plugin development
Jquery plugin development
 
Crash Course to SQL in PHP
Crash Course to SQL in PHPCrash Course to SQL in PHP
Crash Course to SQL in PHP
 
14. CodeIgniter adaugarea inregistrarilor
14. CodeIgniter adaugarea inregistrarilor14. CodeIgniter adaugarea inregistrarilor
14. CodeIgniter adaugarea inregistrarilor
 

Destacado (6)

5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
String matching algorithm
String matching algorithmString matching algorithm
String matching algorithm
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 

Similar a 13. structure

12 2. structure
12 2. structure12 2. structure
12 2. structure
웅식 전
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
Alisha Korpal
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 

Similar a 13. structure (20)

12 2. structure
12 2. structure12 2. structure
12 2. structure
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Structures in C.pptx
Structures in C.pptxStructures in C.pptx
Structures in C.pptx
 
Structures
StructuresStructures
Structures
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptx
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
Ch7 structures
Ch7 structuresCh7 structures
Ch7 structures
 
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
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structures
StructuresStructures
Structures
 
Structures in c
Structures in cStructures in c
Structures in c
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 

Más 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 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(학생)
웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
웅식 전
 

Más 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 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(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 

Último

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
 

Último (20)

Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 48 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 48 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 48 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 48 (Gurgaon)
 
WhatsApp 📞 8448380779 ✅Call Girls In Chaura Sector 22 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Chaura Sector 22 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Chaura Sector 22 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Chaura Sector 22 ( Noida)
 
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...
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Palam Vihar (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Palam Vihar (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Palam Vihar (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Palam Vihar (Gurgaon)
 
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
 
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
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 47 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 47 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 47 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 47 (Gurgaon)
 
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
 
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...
 
AI as Research Assistant: Upscaling Content Analysis to Identify Patterns of ...
AI as Research Assistant: Upscaling Content Analysis to Identify Patterns of ...AI as Research Assistant: Upscaling Content Analysis to Identify Patterns of ...
AI as Research Assistant: Upscaling Content Analysis to Identify Patterns of ...
 
Kishan Reddy Report To People (2019-24).pdf
Kishan Reddy Report To People (2019-24).pdfKishan Reddy Report To People (2019-24).pdf
Kishan Reddy Report To People (2019-24).pdf
 
BDSM⚡Call Girls in Indirapuram Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Indirapuram Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Indirapuram Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Indirapuram Escorts >༒8448380779 Escort Service
 
Julius Randle's Injury Status: Surgery Not Off the Table
Julius Randle's Injury Status: Surgery Not Off the TableJulius Randle's Injury Status: Surgery Not Off the Table
Julius Randle's Injury Status: Surgery Not Off the Table
 
Nara Chandrababu Naidu's Visionary Policies For Andhra Pradesh's Development
Nara Chandrababu Naidu's Visionary Policies For Andhra Pradesh's DevelopmentNara Chandrababu Naidu's Visionary Policies For Andhra Pradesh's Development
Nara Chandrababu Naidu's Visionary Policies For Andhra Pradesh's Development
 
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
 
BDSM⚡Call Girls in Sector 135 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 135 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 135 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 135 Noida Escorts >༒8448380779 Escort Service
 
Verified Love Spells in Little Rock, AR (310) 882-6330 Get My Ex-Lover Back
Verified Love Spells in Little Rock, AR (310) 882-6330 Get My Ex-Lover BackVerified Love Spells in Little Rock, AR (310) 882-6330 Get My Ex-Lover Back
Verified Love Spells in Little Rock, AR (310) 882-6330 Get My Ex-Lover Back
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 

13. structure

  • 2. 2 Declaring Structures § Array와 structure의 차이점 – array • Array의 모든 element는 같은 type이여야 한다. • Index를 사용하여 각 element를 access한다. – structure • 다른 type의 element로 구성 될 수 있다. • 각 element는 name을 갖는다. • Name에 의해 각 element를 access한다.
  • 3. 3 Declaring Structures § struct declaration – Collection of members(/elements) [Ex] struct student { /* 2 elements로 구성된 structure */ int std_id; char name [20] ; } ; int std_id, char name[20] 2 member를 포함하는 student라 불리우는 struct type을 정의
  • 4. 4 Declaring Structures § struct declaration - struct tag: 정의 되는 structure를 지정하기 위한 name. - 한 번 structure tag인 student가 정의되면, 이제 tag를 사용하 여 같은 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: 생략 가능하지만..
  • 5. 5 Accessing a Member § struct member operator ‘.’ – Structure의 각 member를 access하기 위해 ‘.’를 사용한다. [Ex] struct student { int std_id; char name[20]; } ; struct student p ; p.std_id = 1 ; strcpy( p.name, “Monica” ) ; 1 Monica p std_id name
  • 6. 6 Accessing a Member § member operation [Ex] struct student { int std_id; char name[20]; } struct student p ; scanf(“%s”, p.name); p.std_id = 258; /* assignment */ p.std_id++; /* increment */
  • 7. 7 Declaring Structures § Example int main() { int std_id ; char name[20] ; scanf( “%d”, &std_id ) ; scanf( “%s”, name ) ; printf( “%d ”, std_id ) ; scanf( “%sn”, name ) ; return 0; } struct student { int std_id; char name [20] ; } ; int main() { struct student p ; scanf( “%d”, &p.std_id ) ; scanf( “%s”, p.name ) ; printf( “%d ”, p.std_id ) ; printf( “%sn”, p.name ) ; return 0; }
  • 8. 8 Declaring Structures § Initializing Struct variables [Ex] struct student { int std_id; char name [20] ; } ; struct student person1 = { 1, “Gil Dong” } ; struct student person2 = { 2, “Sun Shin” } ; person1 person1.number person1.name 1 Gil Dong person2 person2.number person2.name 2 Sun Shin
  • 9. 9 Structure Pointer § Pointer 선언 [Ex] struct student { int std_id; char name[20]; } ; struct student s={1, “Gil Dong”}, *p ; p = &s ; s 1 Gil Dong p
  • 10. 10 Structure Pointer § Pointer를 통한 member access [Ex] struct student { int std_id; char name[20]; } ; struct student s, *p = &s ; (*p).std_id = 10 ; strcpy( (*p).name, “Sun Shin” ) ; struct student s, *p = &s ; p->std_id = 10 ; strcpy( p->name, “Sun Shin” ) ;
  • 11. 11 Structure Pointer [Ex] struct shape { int x, y ; char name[10] ; }; struct 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 ) ;
  • 12. 12 Structure Pointer [Ex] struct shape { int x, y ; char name[10] ; }; struct 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 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
  • 14. 14 Functions and Assignment § struct 변수에 값 넣기 struct student { int std_id; char name[20]; } p1, p2; struct student p1 = { 1, “Gil Dong”}, p2 ; p2 = p1 ; // OK??? 1 Gil Dong p1 ? ? p2 OK. 원하는 대로 잘 됨
  • 15. 15 Functions and Assignment § struct 변수에 값 넣기 struct student { int std_id; char name[20]; } ; int main() { struct student p1 = { 1, “Gil Dong”}, p2 ; copy_student( p1, p2 ) ; return 0; } void copy_student( struct student p1, struct student p2 ) { p1.std_id = p2.std_id ; strcpy( p1.name, p2.name ) ; } p2의 값은 어떻게 될까?
  • 16. 16 Functions and Assignment § struct 변수에 값 넣기 void copy_student( struct student *p1, struct student *p2 ) { p1->std_id = p2->std_id ; strcpy( p1->name, p2->name ) ; } struct student { int std_id; char name[20]; } ; int main() { struct student p1 = { 1, “Gil Dong”}, p2 ; copy_student( &p1, &p2 ) ; return 0; } p2의 값은 어떻게 될까?
  • 17. 17 Arrays of Structure § Definition [Ex] struct student { int std_id; char name[20]; } ; struct student my_student[100] ; my_student[0] 1 Gil Dong my_student[1] 5 Sun Shin ………
  • 18. 18 Initialization of Structures § Initializing an Array of Structures [Ex] struct student { int std_id ; char name[20] ; } ; struct student my_student[20] = { {1, “Gil Dong”}, {2, “Sun Sin”}, {3, “Kuk Jung”}, : } ;