1 / 10

Structures, Files and Pointers

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.

bayle
Download Presentation

Structures, Files and Pointers

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. Structures, Files and Pointers

  2. 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

  3. 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;

  4. 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

  5. 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

  6. 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

  7. 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()

  8. 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()

  9. 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

  10. Assignment # 6 / Project – 2

More Related