1 / 17

Introduction to Computers and Programming

Introduction to Computers and Programming. Class 24 Structures (structs) Professor Avi Rosenfeld. Structures. Structures are collections of related variables under one name Derived data type – i.e. constructed using objects of other types

arleene
Download Presentation

Introduction to Computers and Programming

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. Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld

  2. Structures • Structures are collections of related variables under one name • Derived data type – i.e. constructed using objects of other types • Unlike arrays, structures can store variables of many different types (heterogeneous and not homogeneous)

  3. Parts of the structure • Keyword struct introduces the structure • Structure tag names the structure definition • Used with keyword struct when you declare variables of the structure type • Members are the variables declared within the structure definition • Must have unique names within the structure definition • In databases, called “fields”

  4. structure definitions keyword struct: tells the compiler you are defining a struct struct student { char nameFirst[10]; char nameLast[10]; int firstExam; int secondExam; int final; }; structure tag: used to name the structure definition members of the structure don't forget the semi-colon

  5. structure definition (cont’d) • The structure definition makes a new type • It does not allocate space in memory to store variables • The structure is another example of a complex (or derived) data type

  6. members • A struct’s members can be simple data types (int, char, etc) or complex data types (arrays, other structs) • No two members of a struct can share the same name • A struct cannot contain another instance of itself (you need to use a pointer for that)

  7. declaring struct variables • Once the struct is defined, we can declare variables of our new type. struct student currentStudent, class[50]; variable to hold one student record keyword struct struct tag array to hold 50 student records

  8. struct comparison • You cannot compare structs using == or != • trying to do so should cause a syntax error • same problems as arrays • Instead you must write code which will compare the structs

  9. Initializing structs • The syntax for initializing structs is similar to the syntax for arrays. struct student currentStudent = {“Bart”, “Simpson”, 55, 64}; • Declares the variable currentStudent and initializes nameFirst, nameLast, firstExam, secondExam with the values provided. final would be initialized to zero because no value was specified.

  10. Accessing members of a struct • We use the struct member operator (dot operator) to access members of a struct (there is another method which uses pointers) • currentStudent.firstExam would reference the value stored in the variable currentStudent, member firstExam • You can use this notation to reference a struct’s values any place you can use other variables (including function parameters)

  11. Passing structs to functions • You can pass entire structs to function. • Unlike arrays, a struct is passed to a function using call by value (in other words, if you change the value of the struct in a function, that change will not be reflected in the calling function)

  12. Makin’ your own variable types • C provides a mechanism through which you can simplify struct usage and enable you to use it as you would another type • Use the keyword typedef

  13. typedef • The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. • The statement: typedef struct student TStudent; defines a new type “TStudent” as a synonym for type struct student. • Once the typedef synonym is defined, you can declare your variables like this: TStudent class[ 50 ] ;

  14. typedef (cont’d) • It is good practice to capitalize the first letter of typedef synonym. • It is better practice to preface it with a capital “T” to indicate it’s one of your new types e.g. • TRobot, TStudent, etc.

  15. Simple Struct Program

  16. An Array of Structs

  17. Structs with functions

More Related