200 likes | 246 Views
Learn the basics of structures, functions, arrays, pointers, self-referential structures, type definition, unions, bit-fields, and more in the C programming language. Find examples, explanations, and practical applications for each concept.
E N D
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
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
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
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
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
Structures and Functions Presented by Quyen Do Van
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
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
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
Pointers to Structures • Example : Writing a program to count the occurrences of each C keyword Presented by Quyen Do Van
Pointers to Structures • Example : Writing a program to count the occurrences of each C keyword using pointer Presented by Quyen Do Van
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
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
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
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
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
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