1 / 84

C Program Design C Structures, Unions, Bit Manipulations and Enumerations

C Program Design C Structures, Unions, Bit Manipulations and Enumerations. 主講人:虞台文. Content. Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions typedef Unions Bitwise Operators Bit Fields Enumeration Constants.

Download Presentation

C Program Design C Structures, Unions, Bit Manipulations and Enumerations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations 主講人:虞台文

  2. Content • Introduction • Structure Definitions • Initializing Structures • Accessing Members of Structures • Using Structures with Functions • typedef • Unions • Bitwise Operators • Bit Fields • Enumeration Constants

  3. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations Introduction

  4. Main Topics • Structure • Collections of related variables (aggregates) under one name • Can contain variables of different data types • Commonly used to define records to be stored in files • Combined with pointers, can create linked lists, stacks, queues, and trees • Union • Allowed data type being overloaded • Bit fields • Enumeration

  5. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations Structure Definitions

  6. Structure Definitions • A structure definition does not reserve space in memory. • Instead, creates a new data type used to define structure variables. struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; };

  7. Define Structure Variables (I) struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; struct PersonalData teacher, students[51], *p;

  8. Define Structure Variables (II) struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; struct PersonalData teacher, students[51], *p; struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; } teacher, students[51], *p; // variable identifier follows type

  9. Define Structure Variables (III) struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; struct PersonalData teacher, students[51], *p; struct PersonalData { char name[namesize]; char address[addresssize]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; } teacher, students[51], *p; // variable identifier follows type typedef struct _tagPersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; } PersonalData; PersonalData teacher, students[51], *p;

  10. struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; struct PersonalData x, teacher, students[51], *p; Valid Operations • Assigning a structure to a structure of the same type x = teacher; teacher = *p; students[i] = x; • Taking the address (&) of a structure p = &students[i]; • Accessing the members of a structure gets(students[0].name); p->YearOfBirth = 88; • Using the sizeof to determine the size of a structure size = sizeof(struct PersonalData); size = sizeof(x);

  11. #include <stdio.h> #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; main() { struct PersonalData student; // Enter the student's record printf("Enter name of the student: "); gets(student.name); printf("Enter address: "); gets(student.address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &student.YearOfBirth, &student.MonthOfBirth, &student.DayOfBirth); printf("\n\n"); // Print out the student record printf("Student name: %s\n", student.name); printf("Address: %s\n", student.address); printf("Bithday: %2d/%2d/%2d\n", student.YearOfBirth,student.MonthOfBirth, student.DayOfBirth); } Example

  12. #include <stdio.h> #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; main() { struct PersonalData student; // Enter the student's record printf("Enter name of the student: "); gets(student.name); printf("Enter address: "); gets(student.address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &student.YearOfBirth, &student.MonthOfBirth, &student.DayOfBirth); printf("\n\n"); // Print out the student record printf("Student name: %s\n", student.name); printf("Address: %s\n", student.address); printf("Bithday: %2d/%2d/%2d\n", student.YearOfBirth,student.MonthOfBirth, student.DayOfBirth); } Example

  13. #include <stdio.h> #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; main() { struct PersonalData student; // Enter the student's record printf("Enter name of the student: "); gets(student.name); printf("Enter address: "); gets(student.address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &student.YearOfBirth, &student.MonthOfBirth, &student.DayOfBirth); printf("\n\n"); // Print out the student record printf("Student name: %s\n", student.name); printf("Address: %s\n", student.address); printf("Bithday: %2d/%2d/%2d\n", student.YearOfBirth,student.MonthOfBirth, student.DayOfBirth); } Example getPersonInfo(&student); showPersonInfo(&student);

  14. // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *); Example // main.c #include <stdio.h> #include "person.h" main() { struct PersonalData student; // Enter the student's record getPersonInfo(&student); printf("\n\n"); // Print out the student record showPersonInfo(&student); }

  15. // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *); Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } // main.c #include <stdio.h> #include "person.h" main() { struct PersonalData student; // Enter the student's record getPersonInfo(&student); printf("\n\n"); // Print out the student record showPersonInfo(&student); }

  16. // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *); Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } // main.c #include <stdio.h> #include "person.h" main() { struct PersonalData student; // Enter the student's record getPersonInfo(&student); printf("\n\n"); // Print out the student record showPersonInfo(&student); }

  17. sizeof Operator structSomeData { charaChar; int anInt; floataFloat; double aDouble; }; sizeof(struct SomeData) = ?

  18. #include <stdio.h> #include <limits.h> #include <float.h> struct SomeData { char aChar; int anInt; float aFloat; double aDouble; }; main() { char mark[]={'M', 'A', 'R', 'K'}; struct SomeData x; printf("sizeof(struct SomeData) = %d\n\n", sizeof(struct SomeData)); x.aChar = 'A'; x.anInt = INT_MAX; x.aFloat = FLT_MAX; x.aDouble = DBL_MAX; printf("Address of x: %p\n", &x); printf("Address of x.aChar: %p\n", &x.aChar); printf("Address of x.anInt: %p\n", &x.anInt); printf("Address of x.aFloat: %p\n", &x.aFloat); printf("Address of x.aDouble: %p\n", &x.aDouble); } sizeof Operator

  19. #include <stdio.h> #include <limits.h> #include <float.h> struct SomeData { char aChar; int anInt; float aFloat; double aDouble; }; main() { char mark[]={'M', 'A', 'R', 'K'}; struct SomeData x; printf("sizeof(struct SomeData) = %d\n\n", sizeof(struct SomeData)); x.aChar = 'A'; x.anInt = INT_MAX; x.aFloat = FLT_MAX; x.aDouble = DBL_MAX; printf("Address of x: %p\n", &x); printf("Address of x.aChar: %p\n", &x.aChar); printf("Address of x.anInt: %p\n", &x.anInt); printf("Address of x.aFloat: %p\n", &x.aFloat); printf("Address of x.aDouble: %p\n", &x.aDouble); } sizeof Operator

  20. sizeof Operator • Because the size of data items of a particular type is machine dependent and because storage alignment considerations are machine dependent, so too is the representation of a structure. structSomeData { charaChar; int anInt; floataFloat; double aDouble; }; sizeof(struct SomeData) = ?

  21. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations Initializing Structures

  22. Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *);

  23. Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } • // main.c • #include <stdio.h> • #include "person.h" • main() • { • struct PersonalData student = { • "Hale-Evans, Ron", • "Seattle, Washington", • 1965, • 6, • 27 • }; • // Print out the student record • showPersonInfo(&student); • } // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *);

  24. Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } • // main.c • #include <stdio.h> • #include "person.h" • main() • { • struct PersonalData persons[] = { • {"Hale-Evans, Ron", "Seattle, Washington", 1965, 6, 27}, • {"Liddell, Alice", "Wonderland", 1852, 5, 4}, • {"Adams, Douglas", "The Galaxy", 1961, 11, 15} • }; • struct PersonalData *p; • int i; • // Print out the records • for(i = 0, p = persons; i < sizeof(persons)/sizeof(struct PersonalData); i++){ • ShowPersonInfo(p++); • printf("\n"); • } • } // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *);

  25. Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } • // main.c • #include <stdio.h> • #include "person.h" • main() • { • struct PersonalData persons[] = { • {"Hale-Evans, Ron", "Seattle, Washington", 1965, 6, 27}, • {"Liddell, Alice", "Wonderland", 1852, 5, 4}, • {"Adams, Douglas", "The Galaxy", 1961, 11, 15} • }; • struct PersonalData *p; • int i; • // Print out the records • for(i = 0, p = persons; i < sizeof(persons)/sizeof(struct PersonalData); i++){ • ShowPersonInfo(p++); • printf("\n"); • } • } // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *);

  26. Example // person.c #include <stdio.h> #include "person.h" void getPersonInfo(struct PersonalData *p) { printf("Enter name: "); gets(p->name); printf("Enter address: "); gets(p->address); printf("Enter birthday: yy/mm/dd: "); scanf("%2d/%2d/%2d", &p->YearOfBirth, &p->MonthOfBirth, &p->DayOfBirth); } void showPersonInfo(struct PersonalData *p) { printf("Name: %s\n", p->name); printf("Address: %s\n", p->address); printf("Bithday: %2d/%2d/%2d\n", p->YearOfBirth, p->MonthOfBirth, p->DayOfBirth); } • // main.c • #include <stdio.h> • #include "person.h" • main() • { • struct PersonalData persons[] = { • {"Hale-Evans, Ron", "Seattle, Washington", 1965, 6, 27}, • {"Liddell, Alice", "Wonderland", 1852, 5, 4}, • {"Adams, Douglas", "The Galaxy", 1961, 11, 15} • }; • struct PersonalData *p; • int i; • // Print out the records • for(i = 0, p = persons; i < sizeof(persons)/sizeof(struct PersonalData); i++){ • ShowPersonInfo(p++); • printf("\n"); • } • } // person.h #define NAMESIZE 50 #define ADDRSIZE 80 struct PersonalData { char name[NAMESIZE]; char address[ADDRSIZE]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; void getPersonInfo(struct PersonalData *); void showPersonInfo(struct PersonalData *);

  27. Initializing Structures • // initialized when defined • struct PersonalData persons[] = { • {"Hale-Evans, Ron", "Seattle, Washington", 1965, 6, 27}, • {"Liddell, Alice", "Wonderland", 1852, 5, 4}, • {"Adams, Douglas", "The Galaxy", 1961, 11, 15} • }; • struct PersonalData classLeader, friend; • // initialized using assignment operator • classLeader = persons[1]; • // Initialized field by field • strcpy(friend.name, "George Lin"); • strcpy(friend.address, "Taipei Taiwan"); • friend.YearOfBirth = 1978; • friend.MonthOfBirth = 12; • friend.DayOfBirth = 24;

  28. Initializing Structures • // initialized when defined • struct PersonalData persons[] = { • {"Hale-Evans, Ron", "Seattle, Washington", 1965, 6, 27}, • {"Liddell, Alice", "Wonderland", 1852, 5, 4}, • {"Adams, Douglas", "The Galaxy", 1961, 11, 15} • }; • struct PersonalData classLeader, friend, *p; • // initialized using assignment operator • classLeader = persons[1]; • // Initialized field by field using a pointer • p = &friend; • strcpy(p->name, "George Lin"); • strcpy(p->address, "Taipei Taiwan"); • p->YearOfBirth = 1978; • p->MonthOfBirth = 12; • p->DayOfBirth = 24;

  29. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations Accessing Members of Structures

  30. Accessing structure members • Dot operator (.) for structure variables struct PersonalData friend; strcpy(friend.name, "George Lin"); strcpy(friend.address, "Taipei Taiwan"); friend.YearOfBirth = 1978; friend.MonthOfBirth = 12; friend.DayOfBirth = 24; • Arrow operator (->) for pointers struct PersonalData friend, *p; p = &friend; strcpy(p->name, "George Lin"); strcpy(p->address, "Taipei Taiwan"); p->YearOfBirth = 1978; p->MonthOfBirth = 12; p->DayOfBirth = 24;

  31. equivalent More on Arrow Operator • Dot operator (.) for structure variables struct PersonalData friend; strcpy(friend.name, "George Lin"); strcpy(friend.address, "Taipei Taiwan"); friend.YearOfBirth = 1978; friend.MonthOfBirth = 12; friend.DayOfBirth = 24; • Arrow operator (->) for pointers struct PersonalData friend, *p; p = &friend; strcpy(p->name, "George Lin"); strcpy(p->address, "Taipei Taiwan"); p->YearOfBirth = 1978; p->friend.MonthOfBirth = 12; p->DayOfBirth = 24; struct PersonalData friend, *p; p = &friend; strcpy((*p).name, "George Lin"); strcpy((*p).address, "Taipei Taiwan"); (*p).YearOfBirth = 1978; (*p).friend.MonthOfBirth = 12; (*p).DayOfBirth = 24;

  32. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations Using Structures with Functions

  33. typedef struct_tagComplex { doublereal; doubleimag; } Complex; ParameterPassings • Passing Value Complex AddByVal(Complex a, Complex b); • Passing Reference void AddByRef(const Complex *a, const Complex *b, Complex *c);

  34. // complex.h typedefstruct _tagComplex { double real; double imag; } Complex; Complex AddByVal(Complex, Complex); void AddByRef(Complex*, Complex*, Complex*); Example // complex.c #include "complex.h" Complex AddByVal(Complex a, Complex b) { Complex c; c.real = a.real + b.real; c.imag = a.imag + b.imag; return c; } void AddByRef(Complex* a, Complex* b, Complex* c) { c->real = a->real + b->real; c->imag = a->imag + b->imag; }

  35. // complex.h typedefstruct _tagComplex { double real; double imag; } Complex; Complex AddByVal(Complex, Complex); void AddByRef(Complex*, Complex*, Complex*); Example // main.c #include <stdio.h> #include "complex.h" main() { Complex x = {12.0, 5}; Complex y = {4.0, 8.0}; Complex z, w; // Call by value z = AddByVal(x, y); printf("(%.1f + i%.1f) + (%.1f + i%.1f) = (%.1f + i%.1f)\n", x.real, x.imag, y.real, y.imag, z.real, z.imag); // Call by reference AddByRef(&x, &y , &w); printf("(%.1f + i%.1f) + (%.1f + i%.1f) = (%.1f + i%.1f)\n", x.real, x.imag, y.real, y.imag, w.real, w.imag); } // complex.c #include "complex.h" Complex AddByVal(Complex a, Complex b) { Complex c; c.real = a.real + b.real; c.imag = a.imag + b.imag; return c; } void AddByRef(Complex* a, Complex* b, Complex* c) { c->real = a->real + b->real; c->imag = a->imag + b->imag; }

  36. Passing arrays by value • Create a structure with the array as a member • Pass the structure

  37. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations typedef

  38. typedef • Creates synonyms for previously defined data types • typedef does not create a new data type • Only creates an alias • Create shorter type names • Make a program more portable. typedeftype-declaration synonym;

  39. Example // complex.h struct _tagComplex { double real; double imag; }; typedef struct _tagComplex Complex; typedef struct _tagComplex *ptrComplex; Complex AddByVal(Complex, Complex); void AddByRef(ptrComplex, ptrComplex, ptrComplex);

  40. Some More Examples // complex.h struct _tagComplex { double real; double imag; }; typedef struct _tagComplex Complex; typedef struct _tagComplex *ptrComplex; Complex AddByVal(Complex, Complex); void AddByRef(ptrComplex, ptrComplex, ptrComplex); // SomeTypeDef.h #include "complex.h" typedefchar CHAR; typedef CHAR* PSTR; typedefunsigned char BYTE; typedefint INT; typedefunsigned UINT; typedef UINT DWORD; typedef Complex (*COMPLEXOP)(Complex, Complex); typedef INT (*ARITHOP)(INT, INT); INT sum(INT a, INT b); INT substract(INT a, INT b); INT mul(INT a, INT b); INT div(INT a, INT b);

  41. Some More Examples /* arith.c */ #include <SomeTypedef.h> INT sum(INT a, INT b) { return a + b; } INT substract(INT a, INT b) { return a - b; } INT mul(INT a, INT b) { return a * b; } INT div(INT a, INT b) { if(b) return a / b; else return 0; } // complex.h struct _tagComplex { double real; double imag; }; typedef struct _tagComplex Complex; typedef struct _tagComplex *ptrComplex; Complex AddByVal(Complex, Complex); void AddByRef(ptrComplex, ptrComplex, ptrComplex); // SomeTypeDef.h #include "complex.h" typedefchar CHAR; typedef CHAR* PSTR; typedefunsigned char BYTE; typedefint INT; typedefunsigned UINT; typedef UINT DWORD; typedef Complex (*COMPLEXOP)(Complex, Complex); typedef INT (*ARITHOP)(INT, INT); INT sum(INT a, INT b); INT substract(INT a, INT b); INT mul(INT a, INT b); INT div(INT a, INT b);

  42. Some More Examples #include <stdio.h> #include "SomeTypedef.h" ARITHOP arith[4]={sum, substract, mul, div}; main() { INT val1, val2, op; printf("Enter two numbers: "); scanf("%d %d", &val1, &val2); printf("0: Add, 1: Sub, 2: Mul, 3: Div\n"); do { printf("Enter number of operation: "); scanf("%d", &op); } while(op<0 || op>3); printf("%d", (*arith[op])(val1, val2)); } // complex.h struct _tagComplex { double real; double imag; }; typedef struct _tagComplex Complex; typedef struct _tagComplex *ptrComplex; Complex AddByVal(Complex, Complex); void AddByRef(ptrComplex, ptrComplex, ptrComplex); // SomeTypeDef.h #include "complex.h" typedefchar CHAR; typedef CHAR* PSTR; typedefunsigned char BYTE; typedefint INT; typedefunsigned UINT; typedef UINT DWORD; typedef Complex (*COMPLEXOP)(Complex, Complex); typedef INT (*ARITHOP)(INT, INT); INT sum(INT a, INT b); INT substract(INT a, INT b); INT mul(INT a, INT b); INT div(INT a, INT b);

  43. Some More Examples #include <stdio.h> #include "SomeTypedef.h" ARITHOP arith[4]={sum, substract, mul, div}; main() { INT val1, val2, op; printf("Enter two numbers: "); scanf("%d %d", &val1, &val2); printf("0: Add, 1: Sub, 2: Mul, 3: Div\n"); do { printf("Enter number of operation: "); scanf("%d", &op); } while(op<0 || op>3); printf("%d", (*arith[op])(val1, val2)); } // complex.h struct _tagComplex { double real; double imag; }; typedef struct _tagComplex Complex; typedef struct _tagComplex *ptrComplex; Complex AddByVal(Complex, Complex); void AddByRef(ptrComplex, ptrComplex, ptrComplex); // SomeTypeDef.h #include "complex.h" typedefchar CHAR; typedef CHAR* PSTR; typedefunsigned char BYTE; typedefint INT; typedefunsigned UINT; typedef UINT DWORD; typedef Complex (*COMPLEXOP)(Complex, Complex); typedef INT (*ARITHOP)(INT, INT); INT sum(INT a, INT b); INT substract(INT a, INT b); INT mul(INT a, INT b); INT div(INT a, INT b);

  44. Exercise • Implement a program similar to the above example but for complex numbers.

  45. C Program DesignC Structures, Unions, Bit Manipulations and Enumerations Unions

  46. union • It is sometimes desirable to define a variable which can be of two or more different types according to different circumstances (overloading). • The distinction between a union and a structure: • The members of a structure define different variables • The members of a union define different manifestations of the same variable. • The space need only be allocated to accommodate the largest type specified in a union.

  47. union Definition typedef union _tagRemark { int age; double tall; char month[4]; } REMARK; REMARK rem; union Remark { int age; double tall; char month[4]; } rem; union Remark { int age; double tall; char month[4]; }; union Remark rem; sizeof(union Remark)=? sizeof(REMARK)=?

  48. Valid union Operations • Assignment to union of same type: = x = y; y = *p; • Taking address: & p = &x; • Accessing union members: . int n = x.age; printf("%s\n", y.month); • Accessing members using pointers: -> int m = p->age; printf("%s\n", p->month); union Remark { int age; double tall; char month[4]; } x, y, *p;

  49. Example // main.c #include <stdio.h> #include <string.h> #include "remark.h" void ShowData(VarRemark data) { switch(data.typeRemark){ case 0: printf("Age = %d\n", data.rem.age); break; case 1: printf("Tall = %f\n", data.rem.tall); break; case 2: printf("Month = %s\n", data.rem.month); break; } } main() { VarRemark datas[3]={{0, 35}, {1}, {2}}; int i; datas[1].rem.tall = 174.5; strcpy(datas[2].rem.month, "Jul"); for(i=0; i< sizeof(datas) / sizeof(VarRemark); i++) ShowData(datas[i]); } // remark.h typedefunion _tagRemark { int age; double tall; char month[4]; } REMARK; typedefstruct _tagVarRemark { int typeRemark;// 0: age // 1: tall // 2: month REMARK rem; } VarRemark; A union may only be initialized with a value of the type of its first member.

  50. Example // main.c #include <stdio.h> #include <string.h> #include "remark.h" void ShowData(VarRemark data) { switch(data.typeRemark){ case 0: printf("Age = %d\n", data.rem.age); break; case 1: printf("Tall = %f\n", data.rem.tall); break; case 2: printf("Month = %s\n", data.rem.month); break; } } main() { VarRemark datas[3]={{0, 35}, {1}, {2}}; int i; datas[1].rem.tall = 174.5; strcpy(datas[2].rem.month, "Jul"); for(i=0; i< sizeof(datas) / sizeof(VarRemark); i++) ShowData(datas[i]); } // remark.h typedefunion _tagRemark { int age; double tall; char month[4]; } REMARK; typedefstruct _tagVarRemark { int typeRemark;// 0: age // 1: tall // 2: month REMARK rem; } VarRemark;

More Related