170 likes | 188 Views
Learn about structures, collections of related variables, heterogeneous data types, struct keywords, initializing & accessing members, and passing structs to functions in programming.
E N D
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 • Unlike arrays, structures can store variables of many different types (heterogeneous and not homogeneous)
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”
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
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
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)
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
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
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.
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)
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)
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
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 ] ;
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.