Professional Documents
Culture Documents
CHAPTER 6
Structure Definitions
A structure is a set of diverse types of data with different
lengths grouped under a unique declaration.
The syntax is: struct model_name {
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;
model_name is a name for the model of the structure type.
object_name is a valid identifier (or identifiers) for structure objects.
Within key brackets { } the types and their names corresponding to
the elements that compose the structure are listed.
Computer Programming 12/20/2022 2
continued
struct Person
{
char name[50];
int age;
float salary;
};
The rules are the same than for fundamental data types,
For example:
struct products {
char name [35];
float price;
} ;
products a_product;
// a_product is an object of struct type products
products* p_product;
// p_product is a pointer to point to objects of products
Computer Programming 12/20/2022 8
Example 3:
#include <iostream> cout << "Enter feet: ";
using namespace std; cin >> (*ptr).feet;
cout << "Enter inch: ";
struct Distance { cin >> (*ptr).inch;
int feet;
float inch; cout << "Displaying
}; information." << endl;
cout << "Distance = "
int main() << (*ptr).feet
{ << " feet "
Distance *ptr, d; << (*ptr).inch
<< " inches" << endl;
ptr = &d;
return 0;
}
int main()
{
Season season;