12 2. structure

599 views
416 views

Published on

Published in: News & Politics, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
599
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

12 2. structure

  1. 1. Structure
  2. 2. 2Declaring 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. 3Declaring Structures struct declaration– Collection of members (elements)[Ex] struct student { /* structure consists of 2 elements */int std_id;char name [20] ;} ;
  4. 4. 4Declaring 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. 5Accessing 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” ) ;}1Monicapstd_idname
  6. 6. 6Accessing a Member Member operationstruct 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. 7. typedef of struct Example7struct 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. 8. typedef of struct Example8typedef 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. 9Initializing Structures Exampletypedef struct student {int std_id;char name [20] ;} Student ;void main() {Student person1 = {1, “Gil Dong”} ;Student person2 = { 2, “Sun Shin” } ;}person1person1.number person1.name1 Gil Dong person2person2.number person2.name2 Sun Shin
  10. 10. 10Structure Pointer Pointer Declarationtypedef struct student {int std_id;char name[20];} Student;void main() {Student s={1, “Gil Dong”};Student *p = &s ;}s1 Gil Dongp
  11. 11. 11Structure Pointer Member access through pointertypedef 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. 12Structure Pointertypedef 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. 13Structure Pointertypedef 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. 14Precedence 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. 15Functions and Assignment Assignment into struct variablestypedef struct student {int std_id;char name[20];} Student;void main() {Student p1 = { 1, “Gil Dong”}, p2 ;p2 = p1 ; // OK???}1Gil Dongp1??p2
  16. 16. 16Functions and Assignment Assignment into struct variablestypedef 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. 17Functions and Assignment Assignment into struct variablestypedef 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. 18Arrays of Structure Definitiontypedef struct student {int std_id;char name[20];} Student;Student my_student[100] ;my_student[0]1 Gil Dongmy_student[1]5 Sun Shin ………
  19. 19. 19Initialization of Structures Initializing an Array of StructuresStudent my_student[20] ={ {1, “Gil Dong”},{2, “Sun Sin”},{3, “Kuk Jung”},:} ;

×