Download
inc 161 computer programming for inc n.
Skip this Video
Loading SlideShow in 5 Seconds..
INC 161 Computer Programming for INC PowerPoint Presentation
Download Presentation
INC 161 Computer Programming for INC

INC 161 Computer Programming for INC

339 Views Download Presentation
Download Presentation

INC 161 Computer Programming for INC

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. INC 161 Computer Programming for INC Lecture 10 Structure & Enumeration

  2. Structure Definition A structure is a collection of members that can be of different data types. Example: struct Student { int id; char name[32]; }; • The keyword struct introduces the definition for structure Student. Student is the structure name (or tag of the structure) and is used to declare variables of the structure type • Student contains two members of type int and array of char.The two members id and name are used to hold the student id and student name.

  3. Memory Storage for struct Student

  4. Declaration of Structure type The syntax of a structure declaration can be fairly complex. The common method to declare structures are presented below. • Declaring a tag name and then using the tag name to declare actual variables. Example: struct Student { int id; char name[32]; }; ... struct Student s; s is a variable of struct Student type with two members id and name.

  5. Declaring a structure without using a tag name. struct { int id; char name[32]; } s; This is useful if the structure is used only in one place. • Declaring a structure with a tag name and variables. struct Student { int id; char name[32]; } s1, s2; Boths1 and s2 are variables of struct Student type.

  6. Pointer to Structures Declaring a pointer to structure is the same as declaring pointers to other objects. struct Student { int id; char name[32]; } *sp1; struct Student *sp2, **spp3; Both sp1 and sp2 are pointers to struct Student type. spp3 is a pointer to pointer to struct Student.

  7. Accessing Structure Members There are two methods to access structure members. The first method is to access a structure member by structure name. Method 1. Using structure variable name, dot operator (.) and the name of the member field. struct Student s; s.id = 101;

  8. /* File: accessmember.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; }; int main() { struct Student s; printf(“Please input id and name\n”); scanf(“%d”, &s.id); scanf(“%s”, s.name); printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); return 0; } > acccessmember.c Please input id and name 102 Doe s.id = 102 s.name = Doe Example: (Accessing structure members by structure name)

  9. Accessing Structure Members (Cont.) Method 2. The second method to access a structure member is through a pointer to the structure. Using a pointer to a structure, the arrow operator (->) and the name of a member field. struct Student s, *sp; sp = &s; ... printf("sp->id = %d\n", sp->id); The symbol “sp->id” is equivalent to (*sp).id

  10. /* File: spointer.c */ #include <stdio.h> struct Student { int id; char name[32]; }; int main() { struct Student s, *sp; sp = &s; sp->id = 101; printf(“Please input id and name\n”); scanf(“%d”, &sp->id); scanf(“%s”, sp->name); printf("sp->id = %d\n", sp->id); printf("sp->name = %s\n", sp->name); return 0; } > spointer.c Please input id and name 102 Smith sp->id = 102 sp->name = Smith Example: (Accessing structure members through a pointer to the structure)

  11. Typedef for Structure Type By using typedef, we can omit the “struct” keyword. typedef struct { int id; char name[32]; } student_t; ... student_t s; Or struct Student { int id; char name[32]; }; ... typedef struct Student student_t; ... student_t s; // struct Student s; student_t is the type of struct Student.

  12. Typedef for Structure Type(Cont.). typedef struct Student { int id; char name[32]; } student_t, *studentptr_t; ... student_t s; // struct Student s; studentptr_t sptr; // struct Stuednt *sptr; student_t is the type of struct Student. studentptr_t is the type of pointer to struct Student.

  13. /* File: typedefs.c */ #include <stdio.h> #include <string.h> typedef struct Student { int id; char name[32]; } student_t; int main() { student_t s; s.id = 101; strcpy(s.name, "John"); printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); return 0; } Output: s.id = 101 s.name = John

  14. Structure Initialization Two common ways to initialize the structures. 1) struct Student { int id; char name[32]; } s={101, "John"}; 2) struct Student s = {102, "Doe"}; Following example uses above two methods to initialize two variables of struct Student type and print out the results. Variable s is a global variable.

  15. /* File: initial.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; } s={101, "John"}; int main() { struct Student s2 = {102, "Doe"}; printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); printf("s2.id = %d\n", s2.id); printf("s2.name = %s\n", s2.name); return 0; } Example: Output: s.id = 101 s.name = John s2.id = 102 s2.name = Doe

  16. Size of Structures The size of a structure can be calculated by sizeof() operator. The argument of operator sizeof() can be the struct type or the variable of struct type. The example below prints out the size of struct Student. /* File: sizeofs.c */ #include <stdio.h> struct Student { int id; char name[32]; } s, *sp; int main() { printf("sizeof(struct Student) = %u\n", sizeof(struct Student)); printf("sizeof(s) = %u\n", sizeof(s)); printf("sizeof(struct Student*) = %u\n", sizeof(struct Student*)); printf("sizeof(sp) = %u\n", sizeof(sp)); return 0; } Output: sizeof(struct Student) = 36 sizeof(s) = 36 sizeof(struct Student*) = 4 sizeof(sp) = 4

  17. Assigning and Comparing Structures /* File: assign.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; } s1 = {101, "John"}; int main() { struct Student s, *sp, *sp1; s = s1;// structure assignment printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); sp = &s; sp1 = &s1; if(sp == sp1) printf("sp and sp1 point to the same object.\n"); else printf("sp and sp1 point to different objects.\n"); if(s.id == s1.id && !strcmp(s.name, s1.name)) printf("The contents of s and s1 are the same.\n"); return 0; }

  18. Assigning and Comparing Structures The assignment operator ‘=‘ can be applied to both structure and pointer to structure. A structure can be assigned to a structure variable, provided that they are of the same structure type. The value for each member of the structure of the rvalue will be assigned to the corresponding member of the lvalue. The relational operators ‘= =‘ and ‘!=“ can be applied to variables of pointer to structure type, not to variables of structure type. To compare variables of structure type, compare their corresponding member fields. Output: s.id = 101 s.name = John sp and sp1 point to different objects. The contents of s and s1 are the same.

  19. Array of Structures Because a structure is a data object, it is possible to create arrays of structure. Each element of the array has structure data type. An array of structure can declared by the same method for declaring a structure variable. Following example declares and initializes an array of structure with three elements. The values of elements of the array are also printed out.

  20. /* File: arrays.c */ #include <stdio.h> struct Student { int id; char name[32]; }; int main() { struct Student s[3] = {{101, "John"}, {102, "Doe"}, {103, “Peter"}}; int i, num; printf(“Number of elements is %d\n”, num); for(i=0; i<3; i++) { printf("s[%d].id = %d\n", i, s[i].id); printf("s[%d].name = %s\n", i, s[i].name); } return 0; } Output: Number of elements is 3 s[0].id = 101 s[0].name = John s[1].id = 102 s[1].name = Doe s[2].id = 103 s[2].name = Peter

  21. Memory layout and contents of structure array s

  22. /* File: arraysp.c */ #include <stdio.h> struct Student { int id; char name[32]; }; int main() { struct Student *sp, s[3] = {{101, "John"}, {102, "Doe"}, {103, "Peter"}}; int i; sp = s; for(i=0; i<3; i++) { printf("sp[%d].id = %d\n", i, sp[i].id); printf("sp[%d].name = %s\n", i, sp[i].name); } for(i=0; i<3; i++, sp++) { printf("id = %d\n", sp->id); printf("name = %s\n", sp->name); } return 0; } Example • Pointer sp is treated as array in the for-loop. • Pointer sp is advanced to the next element by expression sp++ in the while-loop. Output: sp[0].id = 101 sp[0].name = John sp[1].id = 102 sp[1].name = Doe sp[2].id = 103 sp[2].name = Peter id = 101 name = John id = 102 name = Doe id = 103 name = Peter

  23. Passing Structures as Function Arguments Two methods to pass structures as arguments • Pass by value (passes the structure itself) • Passes an entire copy of the structure • Pass by reference (passes a pointer to the structure) • Passes the address of a structure This example illustrates above two methods to pass a structure to a function.

  24. Output: Program: /* File: funcstructarg.c */ #include <stdio.h> struct Student { int id; char name[32]; } s1={101, "John"}; void func(struct Student s, struct Student *sp){ printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); printf("sp->id = %d\n", sp->id); printf("sp->name = %s\n", sp->name); } int main() { struct Student s2 = {102, "Doe"}; func(s1, &s2); return 0; } s.id = 101 s.name = John sp->id = 102 sp->name = Doe

  25. Function Returning Structures It is possible to return a structure from functions. The return type of the function must agree with the actual returned value. For example, struct tag funct1() { /* Define a function that */ /* returns a structure */ struct tag s; ... return s; }

  26. Output: Example: (Function returning a structure) /* File: returns.c */ #include <stdio.h> #include <string.h> /* for strcpy() */ struct Student { int id; char name[32]; }; struct Student func(int id) { struct Student s1; s1.id = id+1; strcpy(s1.name, “Doe”); return s1; // return structure } int main() { struct Student s = {101, “John"}, s1; s1 = func(s.id); // struct assignment printf("s1.id = %d\n", s1.id); printf(“s1.name = %s\n", s1.name); return 0; } s1.id = 102 s1.name = Doe

  27. Function Returning a Pointer to a Structure It is also possible to return a pointer to a structure from a function. The return type of the function shall agree with the actual returned value. For example, funct2() returns the address of global variable. struct tag s; struct tag *funct2() { /* Define a function that */ /* returns a pointer to a structure */ ... return &s; } When a function passes or returns a pointer, the pointer shall point to a global or static variable, or dynamically allocated memory, valid memory for variables of automatic storage duration..

  28. Output: Example2: (Function returning a pointer to a structure) /* File: returnsptr.c */ #include <stdio.h> struct Student { int id; char name[32]; }; struct Student *func(struct Student *sp) { sp->id = 102; return sp; // sp is a valid memory } int main() { struct Student *sp, s ={101, "John"};; sp = func(&s); // sp = &s; printf("sp->id = %d\n", sp->id); printf("sp->name = %s\n", sp->name); return 0; } sp->id = 102 sp->name = John

  29. Function cannot return a pointer to a local variable: The example below returns the address of s1, which is a local variable. This address will be unreferenced after finish the function. /* File: returnsptr_err.c */ #include <stdio.h> struct Student { int id; char name[32]; } s={101, "John"}; struct Student *func() { struct Student s1; s1 = s; s1.id = 102; return &s1; // Error: return a pointer to local variable } int main() { struct Student *sp; sp = func(); printf("sp->id = %d\n", sp->id); printf("sp->name = %s\n", sp->name); return 0; }

  30. Nested Structures It is called a nested structure when one of the members of a structure is a structure also. For example, struct Student is a nested structure because the member birthday is a structure with struct Birthday type. struct Birthday { short day, month, year; }; struct Student { int id; char name[32]; struct Birthday birthday; } s, *sp; The field day, month, and year can be accessed by the following format. s.birthday.day sp->birthday.day s.birthday.month sp->birthday.month s.birthday.year sp->birthday.year

  31. Output: /* File: nested.c */ #include <stdio.h> #include <string.h> struct Birthday { short day, month, year; }; struct Student { int id; char name[32]; struct Birthday birthday; }; int main() { struct Student s, *sp; sp = &s; s.id = 101; strcpy(s.name, "John"); /* September 16, 1990 */ s.birthday.day = 16; s.birthday.month = 8; sp->birthday.year = 1990; // s.birthday.year = 1990; printf("id = %d\n", s.id); printf("name = %s\n", s.name); printf("birthday = %d\n", s.birthday.day); printf("birthd month = %d\n", s.birthday.month); printf("birth year = %d\n", sp->birthday.year); return 0; } id = 101 name = John birthday = 16 birth month = 8 birth year = 1990 Example

  32. Nested Structures The struct Staff is a nested structure. The member birthday of struct Staff is a pointer to structure Birthday. struct Birthday { short day, month, year; }; struct Student { int id; char name[32]; struct Birthday *birthday; } s, *sp; The field day, month, and year can be accessed by the following format. s.birthday->day sp->birthday->day s.birthday->month sp->birthday->month s.birthday->year sp->birthday->year

  33. Output: • /* File: nestedpointer.c */ • #include <stdio.h> • #include <string.h> • struct Birthday { • short day, month, year; • }; • struct Staff{ • int id; • char name[32]; • struct Birthday *birthday; • }; • int main() { • struct Staff s; • struct Birthday bday; • s.id = 101; • strcpy(s.name, "John"); • s.birthday = &bday; • s.birthday->day = 16; • s.birthday->month = 8; • s.birthday->year = 1990; • printf("id = %d\n", s.id); • printf("name = %s\n", s.name); • printf("birthday = %d\n", s.birthday->day); • printf("birth month = %d\n", s.birthday->month); • printf("birth year = %d\n", s.birthday->year); • return 0; • } id = 101 name = John birthday = 16 birth month = 8 birth year = 1990 Example

  34. Enumerations • An enumerated type is a set of integer values represented by enumeration constants. • For example, enum datatypes { inttype; // 0 floattype; // 1 doubletype; // 2 } d1, d2; enum datatypes d3; enum datatypes d4 = floattype; d3 = inttypes; creates a new enumerated type, enum datatypes, and two variables of the enumerated type d1 and d2. • By default, the first enumeration constant is 0 while the subsequent enumeration constants receive an integer value one greater than the previous enumeration constant.

  35. So if, d1 = inttype; d2 = doubletype; then d1 would have a value of 0 and d2 a value of 2. • An explicit integer value can be associated with an enumeration constant in the definition of an enumerated type. Example: enum datatypes { inttype; // 0 floattype = 10; // 10 doubletype; // 11 };