Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.

Similar presentations


Presentation on theme: "Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that."— Presentation transcript:

1 Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that are usually of a number of different types. A structure is a data type which defines a particular kind of object of your choosing. Let’s think the information needed to describe something as simple as a book. We could declare a structure type to accommodate this:

2 Understanding Structures tMyn2 struct Book { char title[80]; char author[80]; char publisher[80]; int year; }; This declaration does not define any variables; it specifies a new type called Book. The keyword struct declares that Book is a structure.

3 Understanding Structures tMyn3 The elements enclosed between the braces in the definition are usually referred to as data members of the structure Book. Every variable of type Book will contain the members title, author, publisher and year. The amount of memory that you need to store a structure object is the aggregate of the memory needed to store each of the data members. The data members of a structure can be of any type, except the type of the structure being defined. The next three statements define three variables:

4 Understanding Structures tMyn4 Book novel; Book* pTravelGuide; Book languageGuide[10]; The variable novel is of type Book, pTravelGuide is of type pointer to Book, and languageGuide is an array with 10 elements of type Book. The first way to get data into the members of a structure variable is to define the initial values for the data members in the definition:

5 Understanding Structures tMyn5 Book paperback= { “Beginning C++ The Complete Guide”, “Ivor Horton”, “Wrox Press Ltd.”, 2004 }; The initializing values appear between a set of braces, separated by commas. A structure type that can be initialized with a list of values between braces is called an aggregate – this simply means that the structure type is composed of built-in data types, arrays and other aggregates.

6 Understanding Structures tMyn6 If you supply fewer initial values in the declaration than there are data members of the structure variable, then data members without initial values will be initialized to 0.


Download ppt "Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that."

Similar presentations


Ads by Google