1 / 19

The programming C language

The programming C language. Structures. Presented by Quyen Do Van& Thanh Do Tien. Feb2012. Contents. Basics of Structures Structures and Functions Arrays of Structures Pointers to Structures Self-referential Structures Table Lookup Typedef Unions Bit-fields. Presented by Quyen Do Van.

vjack
Télécharger la présentation

The programming C language

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. The programming C language Structures Presented by Quyen Do Van& Thanh Do Tien Feb2012

  2. Contents • Basics of Structures • Structures and Functions • Arrays of Structures • Pointers to Structures • Self-referential Structures • Table Lookup • Typedef • Unions • Bit-fields Presented by Quyen Do Van

  3. Basics of Strutures • A Structure is a collection of related data items,possibly of different types • Structures hold data that belong together • Examples: • Student record: student id, name, major, gender, start year, … • Bank account: account number, name, currency, balance, … • Address book: name, address, telephone number, … Presented by Quyen Do Van

  4. Basics of Strutures • Definition of a structure: struct <struct-type> { <type> <identifier_list>; <type> <identifier_list>; ... } ; • Example: struct point { int x; int y; } ; Each identifierdefines a memberof the structure The “Point” structure has 2 members, x & y Presented by Quyen Do Van

  5. Basics of Strutures • A structure can be initialized by following its definition with a list of initializers, each a constant expression, for the members struct point = {3, 4}; • The members of a struct type variable are accessed with the dot (.) operator <struct-variable>.<member_name>; struct point pt; ... printf(“(%d,%d)”,pt.x,pt.y); Presented by Quyen Do Van

  6. Basics of Strutures • Structures can be nested struct rect { struct point pt1; struct point pt2; } ; ... struct rect screen; ... screen.pt1.x; The rect structure contains two point structures Presented by Quyen Do Van

  7. Structures and Functions Presented by Quyen Do Van

  8. Arrays of Structures • An ordinary array: One type of data 0 1 2 … 98 99 • An array of structs: Multiple types of data in each array element 0 1 2 … 98 99 point x y Presented by Quyen Do Van

  9. Arrays of Structures • Declaration struct point struct point { { int x; int x; int y ; int y; } arrayPoint[NKEYS]; } arrayPoint[] = { 1, 2, struct point 3, 4, { 5, 6, int x; 7, 8, int y; 9, 10 }; }; struct point arrayPoint[NKEYS]; Presented by Quyen Do Van

  10. Pointers to Structures • Declaration struct point { int x; int y; }; struct point *p; • Access to members p->x; p->y; x P y Presented by Quyen Do Van

  11. Pointers to Structures • Example : Writing a program to count the occurrences of each C keyword Presented by Quyen Do Van

  12. Pointers to Structures • Example : Writing a program to count the occurrences of each C keyword using pointer Presented by Quyen Do Van

  13. Self-referential Structures • Self-referential Structures are regular used as data structures. • Example: List, queue, binary tree… struct tnode { char * name; struct tnode *left; struct tnode *right; } Presented by Thanh Do Tien

  14. Table lookup • This is a pointer table that store element and the replacement element. When the element is found , it will be replace by replacement element. • Example struct nlist { struct nlist *next; char *name; char *defn; } Presented by Thanh Do Tien

  15. Type def • Type def is used for creating new data type names. • Example Typedef char *String; Typedef struct node { int data; struct node * next; } node, *node_ptr; Presented by Thanh Do Tien

  16. UNION • A union is a variable that may hold objects of different types and size, with the compiler keeping track of size and alignment requirements. • Member union accessed • Union-name.member • Union-pointer->member Example union u_tag { int ival; float fval; }u; Presented by Thanh Do Tien

  17. union • Notice • When changing the value of ival ,the value of fval maybe change because ival and fval are stored in a same memory. • If the variable utype is used to keep track of the current type stored in u, then one might see code such as if (utype==INT) printf(“%d”,u.ival); if (utype==FLOAT) printf(“%f”,u.fval); Presented by Thanh Do Tien

  18. Bit fields • I can declare Enum { KEYWORD=01, EXTERNAL =02, STATIC =04}; When I want to turn on or turn off bits of flag we use : flag |= EXTERNAL|STATIC; flag &= ~(EXTERNAL|STATIC); • Other way Struct { unsigned int is_keyword :1; unsigned int is_extern :1; unsigned int is_static :1; }flag; Then we can do : flag.is_extern = flag.is_static = 1; flag.is_extern = flag.is_static = 0; Presented by Thanh Do Tien

  19. Thank you

More Related