Structure
2
Declaring Structures
§ Array와 structure의 차이점
– array
• Array의 모든 element는 같은 type이여야 한다.
• Index를 사용하여 각 element를 access...
3
Declaring Structures
§ struct declaration
– Collection of members(/elements)
[Ex] struct student { /* 2 elements로 구성된 st...
4
Declaring Structures
§ struct declaration
- struct tag: 정의 되는 structure를 지정하기 위한 name.
- 한 번 structure tag인 student가 정의되...
5
Accessing a Member
§ struct member operator ‘.’
– Structure의 각 member를 access하기 위해 ‘.’를 사용한다.
[Ex] struct student {
int ...
6
Accessing a Member
§ member operation
[Ex] struct student {
int std_id;
char name[20];
}
struct student p ;
scanf(“%s”, ...
7
Declaring Structures
§ Example
int main()
{
int std_id ;
char name[20] ;
scanf( “%d”, &std_id ) ;
scanf( “%s”, name ) ;
...
8
Declaring Structures
§ Initializing Struct variables
[Ex] struct student {
int std_id;
char name [20] ;
} ;
struct stude...
9
Structure Pointer
§ Pointer 선언
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student s={1, “Gil Dong”}, *p...
10
Structure Pointer
§ Pointer를 통한 member access
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student s, *p...
11
Structure Pointer
[Ex] struct shape {
int x, y ;
char name[10] ;
};
struct shape s, *p = &s;
scanf (“%d %d %s”, &p->x ,...
12
Structure Pointer
[Ex] struct shape {
int x, y ;
char name[10] ;
};
struct shape s, *p = &s;
scanf (“%d %d %s”, &(*p).x...
13
Precedence and Associativity of Operators
( ) [ ] . -> ++(postfix) --(postfix) left to right
++(prefix) --(prefix) ! ~ ...
14
Functions and Assignment
§ struct 변수에 값 넣기
struct student {
int std_id;
char name[20];
} p1, p2;
struct student p1 = { ...
15
Functions and Assignment
§ struct 변수에 값 넣기
struct student {
int std_id;
char name[20];
} ;
int main() {
struct student ...
16
Functions and Assignment
§ struct 변수에 값 넣기
void copy_student( struct student *p1,
struct student *p2 )
{
p1->std_id = p...
17
Arrays of Structure
§ Definition
[Ex] struct student {
int std_id;
char name[20];
} ;
struct student my_student[100] ;
...
18
Initialization of Structures
§ Initializing an Array of Structures
[Ex] struct student {
int std_id ;
char name[20] ;
}...
Upcoming SlideShare
Loading in...5
×

13. structure

122

Published on

Published in: News & Politics, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
122
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

13. structure

  1. 1. Structure
  2. 2. 2 Declaring Structures § Array와 structure의 차이점 – array • Array의 모든 element는 같은 type이여야 한다. • Index를 사용하여 각 element를 access한다. – structure • 다른 type의 element로 구성 될 수 있다. • 각 element는 name을 갖는다. • Name에 의해 각 element를 access한다.
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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”}, : } ;
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×