270 likes | 281 Views
Learn how structures organize data in array-like groups with the same or different types. Explore structure definition, declaration, pointers, member access, typedefs, and initialization methods in C programming.
E N D
INC 161 , CPE 100Computer Programming Lecture 13 Structure
How large data are organized? array Group of data, same type structure Group of data, different types class Group of data and functions
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.
Memory Storage for struct Student struct Student { int id; char name[32]; }; Structure name = Student Members = 2 (1 integer + 1 array)
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.
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.
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.
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.(Dot Operator) Using structure variable name, dot operator (.) and the name of the member field. struct Student s; s.id = 101;
/* File: accessmember.c */ #include <stdio.h> struct Student { int id; char name[32]; }; 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); } > acccessmember.c Please input id and name 102 Doe s.id = 102 s.name = Doe Example: (Accessing structure members by structure name)
Accessing Structure Members (Cont.) Method 2. (Arrow Operator) 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
/* File: spointer.c */ #include <stdio.h> struct Student { int id; char name[32]; }; main() { struct Student s, *sp; sp = &s; 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); } > 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)
Typedef for Structure Type By using typedef, we can omit the “struct” keyword. struct Student { int id; char name[32]; }; ... typedef struct Student student_t; ... student_t s; // struct Student s; or typedef struct { int id; char name[32]; } student_t; ... student_t s; typedef makes student_t equivalent to struct student
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.
/* File: typedefs.c */ #include <stdio.h> #include <string.h> typedef struct Student { int id; char name[32]; } student_t; 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); } Output: s.id = 101 s.name = John
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.
/* File: initial.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; } s={101, "John"}; 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); } Example: Output: s.id = 101 s.name = John s2.id = 102 s2.name = Doe
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; 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)); } Output: sizeof(struct Student) = 36 sizeof(s) = 36 sizeof(struct Student*) = 4 sizeof(sp) = 4
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.
Assigning and Comparing Structures /* File: assign.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; } s1 = {101, "John"}; 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"); }
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.
/* File: arrays.c */ #include <stdio.h> struct Student { int id; char name[32]; }; 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); } } 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
/* File: arraysp.c */ #include <stdio.h> struct Student { int id; char name[32]; }; 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); } } 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
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 { int 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
/* File: nested.c */ #include <stdio.h> #include <string.h> struct Birthday { int day, month, year; }; struct Student { int id; char name[32]; struct Birthday birthday; }; 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); } Output: id = 101 name = John birthday = 16 birth month = 8 birth year = 1990 Example
Nested Structures The struct Student is a nested structure. The member birthday of struct Student is a pointer to structure Birthday. struct Birthday { int 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
/* File: nestedpointer.c */ #include <stdio.h> #include <string.h> struct Birthday { int day, month, year; }; struct Student{ int id; char name[32]; struct Birthday *birthday; }; main() { struct Student s, *sp; struct Birthday bday; • sp = &s; 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); } Output: id = 101 name = John birthday = 16 birth month = 8 birth year = 1990 Example