130 likes | 270 Views
Structures, Files and Pointers. Review - Defining Structures. The following is a definition of a new type typedef struct { char name[64]; char course[128]; int year; } student_info ; student variables of type student can be declared as follows.
E N D
Review - Defining Structures • The following is a definition of a new type typedefstruct { char name[64]; char course[128]; int year; } student_info; • student variables of type student can be declared as follows. • student_info student_1; // same as declaring int year; // all structures declared as of type student_info will have members // name, course and year
Review - Accessing members of structures • All members of a structure behave exactly the way they would if they were being used independently • However, to access members of a structure we have to use the . (dot) operator • Consider the above declaration: student_info student_1; • To assign values to its members, the syntax is: strcpy(student_1.name, “John”); strcpy(student_1.course, “COP 3223H”); student_1.year = 2014;
Example – Files and structures • Write a program to declare a new data type called student_info using the struct and typedef keywords. • The structstudent_info should have student’s name, course and graduation year as members. • Create an input file that contains information about all students’ names, courses and graduation years • Your program should next read all data from the input file in an array of student_info structures • Print all data on screen
Structures and Pointers • Just as with any other data type, pointers can contain the address of a structure • When using a pointer to variable, we can access the original contents of the variable with the de-referencing operator * e.g. int i = 20; int *ptr = &i; //notice the context of * *ptr = 30; // changes the contents of variable i • Similarly, a pointer can contain the address of a structure e.g. student_info*student; //assuming student_info is //already declared as a struct
Structures and Pointers • How do you access structure elements through a pointer variable ??? • To de-reference members of a structure through a pointer to structure, we use the member selector operator -> e.g. student_info *B = &student_1; B->year = 2014; // use the member selector //operator -> to access a struct’s //member through its pointer
An array of structures student_1[2] 2015 cot4226 jake 1500 2014 1500 cop3223h john 1000 1000 1000 student_1 [2] A [2] is the alias for student_1 array in the function print_student_info()
Pointer to a single structure in an array of structures student_1[2] 2015 cot4226 jake 1500 2014 1500 cop3223h john 1000 1000 1000 student_1 [2] 1000 B holds &student_1[0] A [2] is the alias for student_1 array in the function print_student_info()
C is an array of pointers holding addresses of multiple structures in student_1[2] 2015 cot4226 jake 1500 1500 2014 cop3223h 3003 1500 3000 1000 john 1000 3000 C 1000 student_1 [2] C [2] is the alias for student_ptr_array in the function print_student_info() A [2] is the alias for student_1 array in the function print_student_info() B holds &student_1[0] 1000 1000