Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C
Senem Kumova Metin Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under a single name Examples : real number && imaginary number complex number ( 3+5i ) height && width && length rectangular prism
Senem Kumova Metin Defining Structures /* DEFINITION OF RECTANGULAR PRISM */ struct rectangular_prism {int height; int width; int length;}; // name of new type?? /* DEFINITION OF STUDENT RECORD */ struct student_record { int ID; char name[100]; };
Senem Kumova Metin Structures : Creating objects struct complex {int real; int imaginary;}; // create an object struct complex s; struct complex * p= (struct complex *) malloc( sizeof(struct complex)); // create an array of objects struct complex a[4]; struct complex * p= (struct complex *) malloc( sizeof(struct complex)*4);
Senem Kumova Metin Structures : Accessing members of structures 1 struct complex {int real; int imaginary;}; struct complex s; // access members s.real=5; s.imaginary =3; struct complex * p= (struct complex *) malloc( sizeof(struct complex)); // access members p->real=5; p->imaginary=3;
Senem Kumova Metin Structures : Accessing members of structures 2 struct complex {int real; int imaginary;}; int i; struct complex a[4]; struct complex * p= (struct complex *) malloc( sizeof(struct complex)*4); for(i=0;i<4;i++) { a[i].real=i; p[i].real=i; a[i].imaginary=i; p[i].imaginary=i; }
Senem Kumova Metin Structures : DECLARATION ALTERNATIVES struct record { int ID; char * name; char grade; }; struct record s1; struct record { int ID; char * name; char grade; } s1, s2; struct { int ID; char * name; char grade; } s1, s2; struct record { int ID; char * name; char grade; }; typedef struct record rec; typedef struct { int ID; char * name; char grade; } rec; rec s1;
Senem Kumova Metin Structures as Function Arguments Examples Example1. Call by value Example2. Call by value Example3. Call by reference Example4. Arrays
Senem Kumova Metin Example 1 : Define functions // A function to add two integers int sum(int a, int b) { int result= a+b; return result;} struct complex { int r; int i; }; // A function to add two complex numbers struct complex sum ( struct complex a, struct complex b ) { struct complex result; result.r=a.r+b.r; result.i=a.i+b.i; return result;} // A function to print out a complex number void my_print ( struct complex a) { printf(“%d+%di\n”, a.r, a.i );}
Senem Kumova Metin Example 1 : Define main() // Structure definition struct complex { int r; int i; } ; // function prototypes struct complex sum ( struct complex a, struct complex b ); void my_print ( struct complex a); main() { struct complex e1={2,3}; struct complex e2 ={1,2}; struct complex e3; my_print(e1); my_print(e2); e3=sum(e1, e2); my_print(e3);} void my_print ( struct complex a) { printf(“%d+%di\n”, a.r, a.i );}
Senem Kumova Metin Example 2 Define a date structure day - month – year Define a function to find the next day TODAY NEXT DAY –5 – 1 – 2009 6 – – – – 2009 1 – In main create an object of today and call function to find next day
Senem Kumova Metin Example 2 typedef struct { int day; int month; int year; } date ; // structure definition date calculate ( date d ) // function definition {date n; if( d.day==30) if(d.month==12) { n.day=1; n.month=1; n.year=d.year+1; } else { n.day=1; n.month=d.month+1; n.year=d.year; } else {n.day=d.day+1; n.month=d.month; n.year=d.year; } return n;} main()// main function { date today ={12, 10, 2005}; date next_day; next_day= calculate(today); printf(“Next day is %d.%d.%d”, next_day.day,next_day.month,next_day.year); }
Senem Kumova Metin Example 3 (Call by reference) : Define functions // A function to increase an integers void increase_i (int *a) { *a=*a+1;} typedef struct { int n; int d; } fraction; // fraction=n/d // A function to increase a fraction void increase_f (fraction * a ) { a->n= a->n +a->d; } // n/d+1 = (n+d) /d
Senem Kumova Metin Example 3 (Call by reference): Define functions // A function to swap integers void swap_i (int *a, int * b) { int tmp; tmp=*a; *a=*b; *b=tmp;} typedef struct { int n; int d; } fraction; // A function to swap fractions void swap_f (fraction *a, fraction * b) { fraction tmp; tmp=*a; *a=*b; *b=tmp;}
Senem Kumova Metin Example 3 (Call by reference): Define main() typedef struct { int n; int d; } fraction; void swap_f (fraction *a, fraction * b); void increase_f (fraction * a ); main() { fraction x={3,4}; fraction y={4,5}; increase_f(&x); increase_f(&y); swap_f(&x,&y); swap_f(&y,&x); printf(“%d/%d\n”,x.n,x.d); printf(“%d/%d\n”,y.n,y.d);}
Senem Kumova Metin Example 4 : Arrays ( Bookstore example) You will create Bbookstore_system Ask user to enter how many different books he has. You have to store name and total number of copies for each book. Ask user to enter information for each book. Define a function to calculate the number of books in store.
Senem Kumova Metin Example 4 : Arrays void main() { int s, i, total; book * array; printf("How many books do you have?"); scanf("%d",&s); array= (book *)malloc(sizeof( book)*s); for(i=0;i<s;i++) scanf("%d",&(array[i].number)); total=sum(array,s); // sum(&array[0]); printf("total number of books %d",total); } typedef struct { char * name; int number;} book; int sum( book * a, int size) { int i; int r=0; for (i=0;i<size;i++) r=r+a[i].number; return r; } /*int sum( book a[], int size) { int i; int r=0; for (i=0;i<size;i++) r=r+a[i].number; return r; } */
Senem Kumova Metin UNIONS A union –defines a set of alternative values that may be stored in a shared poriton of memory – is similar to structures a collection of variables under a single name Examples : union int _or_float {int i; float f;};
Senem Kumova Metin Example 1: UNIONS union int_or_char {char c; int i; }; int main() { union int_or_char test; test.i= 83; printf("i= %d\n", test.i);// 83 printf("c= %c\n", test.c);// S test.c='A'; printf("i= %d\n", test.i); printf("c= %c\n", test.c); return 0; }
Senem Kumova Metin union int_or_double { /* to be used as an integer OR a double*/ int i; /*needs 4 bytes*/ double d; /*needs 8 bytes*/}; main() { printf ("%d\n", sizeof(int)); printf ("%d\n", sizeof(double)); printf ("%d\n", sizeof(union int_or_double)); } Example 2: UNIONS
Senem Kumova Metin Bit Fields The bit field is used to pack the bits. An int or unsigned member of a structure or union can be declared to consist of a specified number of bits. Such a member is called a bit field, and the number of associated bits is called its width. The width is at most the number of bits in a machine word. The compiler packs the bit fields into a minimal number of machine words
Senem Kumova Metin Example : Bit Fields struct Bits { int a:2; /* 2 bit */ unsigned b:4; /* 4 bits */ int c:1; /* 1 bit */ }; int main() { struct Bits x; x.a=1; x.b=5; x.c=1; printf("a:%d b: %d c:%d\n", x.a, x.b, x.c); return 0; }