Docsity

C++ Programming: Understanding Structures and Data Structures, Slides of Computer Programming

Partial preview of the text

Download C++ Programming: Understanding Structures and Data Structures and more Slides Computer Programming in PDF only on Docsity!

C++ Programming

Structures

docsity.com

Structures

A structure is a data type which defines aparticular kind of object of your choosing.

A data structure is a group of dataelements grouped together under onename.

These data elements, known as

members

can have different types and differentlengths.

docsity.com

Data Structures (Contd.)

The first thing we have to know is that a datastructure creates a new type:

Once a data structure is declared, a new typewith the identifier specified as structure_nameis created and can be used in the rest of theprogram as if it was any other type. Forexample:

docsity.com

Data Structures (Contd.)

Once declared, product has become a new valid type

name like the

fundamental ones int, char or short andfrom that point on we are able to declareobjects (variables) of this compound newtype, like we have done with apple, bananaand melon.

docsity.com

Data Structures (Contd.)

It is important to clearly differentiate betweenwhat is the

structure type name

, and what is

an object

(variable) that has this structure

type.

We can instantiate many objects (i.e. variables,

like apple, banana and melon) from a singlestructure type (product).

docsity.com

Data Structures (Contd.)

Once we have declared our three objects of adetermined structure type (apple, banana andmelon) we can operate directly with theirmembers.

To do that we use a dot (.) inserted betweenthe object name and the member name.

docsity.com

Data Structures (Contd.)

Let's see a real example where you can see howa structure type can be used in the same way asfundamental types:

docsity.com

docsity.com

Data Structures (Contd.)

Therefore, one of the most importantadvantages of data structures is that we caneither refer to their members individually or tothe entire structure as a block with only oneidentifier.

Data structures are a feature that can be usedto represent databases, especially if we considerthe possibility of building arrays of them.

docsity.com

C++ Programming

Structures

docsity.com

Pointers to structures(Contd.)

The value of the pointer pmovie would beassigned to a reference to the objectamovie (its memory address).

We will now go with another examplethat includes pointers, which will serve tointroduce a new operator: the arrowoperator (->):

docsity.com

docsity.com

Pointers to structures(Contd.)

Which is for all purposes equivalent to:

Both expressions pmovie->title and(*pmovie).title are valid and both mean thatwe are evaluating the member title of thedata structure

pointed by

a pointer called

pmovie. It must be clearly differentiatedfrom:

docsity.com

Pointers to structures(Contd.)

which is equivalent to:

And that would access the value pointedby a hypothetical pointer member calledtitle of the structure object pmovie(which in this case would not be apointer).

docsity.com