FAQ | Newsboard | Telnet | Email TA | Lectures | Assignments | Man pages | Help |
---|
C/C++ allow you to define variables made up of other datatypes. Such structures are equivalent to records in pascal and are called a struct in ANSI C/C++.
The general syntax for a struct with tag is:
struct tagname { datatype1 name1; datatype2 name2; datatype3 name3; ... } variable_name;and without a tag:
struct { datatype1 name1; datatype2 name2; datatype3 name3; ... } variable_name;Note that struct always ends with a semicolon (;). Here are a couple of examples:
struct Person { struct { char name[20]; char name[20]; int age; int age; float salary; float salary; } tom; } tom;The above two structs each have three data fields: an array of char, an int, and a float.
struct Person is a datatype and can be used in exactly the same way as we use int, float, or other C++ datatypes. struct Person is not a variable name and to use a struct you must first define variables of that type. This can be done in two ways.
struct Person { char name[20]; int age; float salary; } tom, dick, harry;declares three variables, tom, dick, and harry of type struct Person, each of which contains three data fields.
Person obj1, obj2;
If you had not used a tag name when you originally defined the struct then you will have only one opportunity to declare variables of that structural type.
struct { char name[20]; int age; float salary; } obj3, obj4, obj5;You would not be able to declare any more variables of that particular type at a later time.
A struct variable can be initialized, like any other data variables in C++, at the time it is declared. We could use:
struct Person { char name[20]; int age; float salary; } tom = {"Tom", 25, 40000.50} ;or
Person tom = {"Tom", 25, 40000.50};You CANNOT assign assign values inside the declaration itself. You cannot, for example use
struct Person { char name[20] = {"Tom"}; /* WRONG */ .... } tom;
Although you can only initialize in the aggregrate, you can later assign values to any data fields using the dot (.) notation. To access any data field, place the name of the struct variable, then a dot (.), and then the name of the data field.
Person obj1; obj1.salary = 20677.34;each data field can be individually used in any C++ expression as part of any legal C/C++ statement.
If two struct variables are of the same type, that is, they both were declared to be of the same type (same tag name) then we can copy one variable to another with a simple assignment statement. For example:
Person obj1; Person obj2 = {"Eric", 20, 10000.2}; obj1 = obj2;If both obj1 and obj2 were not declared to be of type struct Person we could not have copied them as simply. For example, we could NOT do this:
struct Person1 { char name[20]; int age; float salary; } obj1 = {"Tom", 25, 40000.50} ; struct Person2 { char name[20]; int age; float salary; } obj2; obj2 = obj1; /* WRONG */even though obj1 and obj2 have exactly the same internal structure.
structs are quite useful and versatile. They can be treated like C++'s predefined data types and can be passed as an argument to a function or returned from a function. Unlike arrays, struct instances are passed by value. A struct's internal fields, its member fields, can individually also be treated just like any variable to be passed as arguments, returned, assigned, or used in expressions. Just remember to use the dot notation to access member fields of a struct.
A struct can be embedded in another struct. More precisely, a struct can have a previously declared struct as a member. This comes in useful when we deal with data structures like linked lists and trees. Here's an example:
typedef char String9[10]; typedef char String15[16]; enum GenderType {male, female, unknown}; enum ClassType { freshman, sophomore,junior, senior, graduate, special }; struct NameType { String15 first; String15 last; char middleInitial; }; struct StudentType { NameType name; ClassType classification; String9 ssn; GenderType gender; float gpa; }; StudentType student;To access a student's middle initial, you have to access a struct that is itself a member of another struct. For example:
student.name.middleInitial = "J";Note that we need to use 2 dots to access an embedded structure. One dot to access the field name and another to access a field within name. In general, we will need to add another dot each time we move to a deeper level of embedding.
An array of structs is declared just like arrays of other data types
in C. I general:
tag arrayname[size]
or for example:
Person business[50];
declares an array of 50 items each of which is of type Person.
It is also common to declare an array when you define a struct:
struct Person { char name[20]; int ID; char phone[10]; } business[50]To reference the array of structs you use a subscript (within []) to access the correct element of the array, then dot notation to access the correct member field. In general:
array_name[subscript].struct_field = valueor specifically:
business[5].ID = 783;We don't usually initialize arrays of structs using the brace syntax, rather, we would probably read in the values from a file.