0% found this document useful (0 votes)
4 views

chapter 8

Chapter 8 of the document covers structures and user-defined data types in computer programming, detailing how to define structures, use pointers, and nest structures. It also explains user-defined types such as typedef, unions, and enumerations, providing examples for each concept. The chapter emphasizes the importance of understanding the distinction between structure models and objects, as well as the memory management aspects of unions.

Uploaded by

bayabayecha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

chapter 8

Chapter 8 of the document covers structures and user-defined data types in computer programming, detailing how to define structures, use pointers, and nest structures. It also explains user-defined types such as typedef, unions, and enumerations, providing examples for each concept. The chapter emphasizes the importance of understanding the distinction between structure models and objects, as well as the memory management aspects of unions.

Uploaded by

bayabayecha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Computer Programming

[ECEg-1052]

Chapter 8:
Structure and User defined data types

Compiled by Amanuel Z.(MSc.)


Outline
 Defining the structure
 Pointers to structures
 Nesting structures
 User defined data types
Defining the structure
 A data structure is a set of diverse types of data that
may have different lengths grouped together under a
unique declaration.
 Its form is the following:
struct model_name {
type1 element1;
type2 element2;
type3 element3;
. .
} object_name;
3
Cont’d...
where
 model_name is a name for the model of the structure
type and the optional parameter object_name is a
valid identifier (or identifiers) for structure object
instantiations.
 Within curly brackets { } they are the types and their
sub-identifiers corresponding to the elements that
compose the structure.

4
Cont’d...
For example:
struct products {
char name [30];
float price;
};
products apple;
products orange, melon;

Once declared, products has become a new valid type
name like the fundamental ones int, char or short and
we are able to declare objects (variables) of that type.
5
Cont’d...
 The optional field object_name that can go at the end
of the structure declaration serves to directly declare
objects of the structure type.
For example we can also declare the structure objects
apple, orange and melon this way:
struct products {
char name [30];
float price;
} apple, orange, melon;

6
Cont’d...
 Once we have declared our three objects of a
determined structure model (apple, orange and
melon) we can operate with the fields that form them.
 To do that we have to use a point (.) inserted between
the object name and the field name.
 For example, we could operate with any of these
elements as if they were standard variables of their
respective types:
apple.name
apple.price
orange.name
orange.price
melon.name
melon.price 7
Cont’d...
 Each one being of its corresponding data type:
 apple.name, orange.name and melon.name are of
type char[30], and
 apple.price, orange.price and melon.price are of
type float.
 Moreover, in cases like the last one in which we took
advantage of the declaration of the structure model to
declare objects of it, the parameter model_name (in
this case products) becomes optional.
 Although if model_name is not included it will not be
possible to declare more objects of this same model
later.
8
Cont’d...
 It is important to clearly differentiate between what is
a structure model, and what is a structure object.
 Using the terms we used with variables, the model is
the type, and the object is the variable.
 We can instantiate many objects (variables) from a
single model (type).

9
Cont’d...
Example
// introduces operator new
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
struct movies_t {
char title [50];
int year;
} mine, yours;
void printmovie (movies_t movie);
int main (){
char buffer [50];
strcpy (mine.title, "2001 A Space Odyssey");
mine.year = 1968;
10
Cont’d...
cout << "Enter title:\n ";
cin.getline (yours.title, 50);
cout << "Enter year:\n ";
cin.getline (buffer,50);
yours.year = atoi (buffer);
cout << "My favourite movie is: "<<printmovie (mine);
cout << "And yours:\n "<<printmovie (yours);
}
void printmovie (movies_t movie){
cout << movie.title<< "\n ";
cout << " (" << movie.year << ")\n";
}
11
Cont’d...
Output:
Enter title: Alien
Enter year: 1979
My favourite movie is: 2001 A Space Odyssey (1968)
And yours: Alien (1979)

12
Cont’d...
Example 2
// array of structures
#include<iostream.h>
#include <stdlib.h>
#define N_MOVIES 5
struct movies_t {
char title [50];
int year;
} films [N_MOVIES];
void printmovie (movies_t movie);
int main (){
char buffer [50];
int n;
13
Cont’d...
for (n=0; n<N_MOVIES; n++) { Output:
cout << "Enter title: "; Enter title: Alien
cin.getline (films[n].title,50); Enter year: 1979
cout << "Enter year: "; Enter title: Blade Runner
cin.getline (buffer,50); Enter year: 1982
Enter title: Matrix
films[n].year = atoi (buffer); Enter year: 1999
} Enter title: Rear Window
cout << "\nYou have entered these movies:\n"; Enter year: 1954
for (n=0; n<N_MOVIES; n++) Enter title: Taxi Driver
printmovie (films[n]); Enter year: 1975
return 0; You have entered these
movies:
}
Alien (1979)
void printmovie (movies_t movie){ Blade Runner (1982)
cout << movie.title; Matrix (1999)
cout << " (" << movie.year << ")\n"; Rear Window (1954)
} Taxi Driver (1975)
14
Pointers to structures
 The rules are the same as for any fundamental data type.
 The pointer must be declared as a pointer to the structure:
struct movies_t {
char title [50];
int year;
};
movies_t amovie;
movies_t * pmovie;
 Here amovie is an object of struct type movies_t and pmovie is
a pointer to point to objects of struct type movies_t.
 So, the following, as with fundamental types, would also be
valid:
pmovie = &amovie;
15
Cont’d...
Example: Output:
// pointers to structures
Enter title: Matrix
#include <iostream.h>
#include <stdlib.h>
Enter year: 1999
struct movies_t {
char title [50]; You have entered:
int year; Matrix (1999)
};
int main (){
char buffer[50];
movies_t amovie;
movies_t * pmovie;
pmovie = & amovie;
cout << "Enter title: ";
cin.getline (pmovie->title,50);
cout << "Enter year: ";
cin.getline (buffer,50);
pmovie->year = atoi (buffer);
cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";
} 16
Cont’d...

The operator -> a reference operator that is used
exclusively with pointers to structures and pointers to
classes.

It allows us not to have to use parenthesis on each
reference to a structure member.
For example we can write:
pmovie -> title
that could be translated to:
(*pmovie).title

Both expressions pmovie -> title and (*pmovie).title
are valid and mean that we are evaluating the element
title of the structure pointed by pmovie.
17
Cont’d...

You must distinguish it clearly from:
*pmovie.title
that is equivalent to
*(pmovie.title)
and that would serve to evaluate the value
pointed by element title of structure movies, that
in this case (where title is not a pointer) it would
not make much sense.

18
Cont’d...

Expression Description Equivalent

Element title of structure


pmovie.title pmovie

Element title of structure


pmovie->title pointed by pmovie (*pmovie).title

Value pointed by element


*pmovie.title title of structure pmovie *(pmovie.title)

19
Nesting structures

Structures can also be nested so that a valid element of
a structure can also be another structure.
struct movies_t {
char title [50];
int year;
}
struct friends_t {
char name [50];
char email [50];
movies_t favourite_movie;
} charlie, maria;
20
Cont’d…
friends_t * pfriends = &charlie;
 Therefore, after the previous declaration we could use
the following expressions:
charlie.name
maria.favourite_movie.title
charlie.favourite_movie.year
pfriends->favourite_movie.year

where, by the way, the last two expressions are
equivalent.

21
User defined data types

We have already seen a data type that is defined by the
user (programmer): the structures.

But in addition to these there are other kinds of user
defined data types:

Definition of own types (typedef)

Unions

Enumerations (enum)

22
Definition of own types (typedef)

C++ allows us to define our own types based on other existing
data types. In order to do that we shall use keyword typedef,
whose form is:
typedef existing_type new_type_name ;
where

existing_type is a C++ fundamental or any other defined type
and

new_type_name is the name that the new type we are going to
define will receive.

typedef can be useful to define a type that is repeatedly used
within a program and it is possible that we will need to change
it in a later version, or if a type you want to use has too long a
name and want it to be shorter.
23
Cont’d...

For example:
typedef char C;
typedef unsigned int WORD;
typedef char * string_t;
typedef char field [50];

In this case we have defined four new data types:

24
Cont’d...

C, WORD, string_t and field as char, unsigned int,
char* and char[50] respectively, that we could
perfectly use later as valid types:
C achar, anotherchar, *ptchar1;
WORD myword;
string_t ptchar2;
field name;

25
Unions

Unions allow a portion of memory to be accessed as
different data types, since all of them are in fact the
same location in memory.

Its declaration and use is similar to the one of
structures but its functionality is totally different:
union model_name {
type1 element1;
type2 element2;
type3 element3;
. .
} object_name;
26
Cont’d...

All the elements of the union declaration occupy the
same space of memory.

Its size is the one of the greatest element of the
declaration.
For example:
union mytypes_t { 
defines three elements:
char c; mytypes.c
int i; mytypes.i
float f; mytypes.f
} mytypes;

each one of a different
data type.

27
Cont’d...
 Since all of them are referring to a same location in memory,
the modification of one of the elements will affect the value of
all of them.
 One of the uses a union may have is to unite an elementary
type with an array or structures of smaller elements.
For example,
union mix_t{
long l;
struct {
short hi;
short lo;
} s;
char c[4];
} mix;
28
Cont’d...
 defines three names that allow us to access the same
group of 4 bytes: mix.l, mix.s and mix.c and which we
can use according to how we want to access it, as
long, short or char respectively.

29
Anonymous unions
 In C++ we have the option that unions be anonymous.
 If we include a union in a structure without any object
name (the one that goes after the curly brackets { })
the union will be anonymous and we will be able to
access the elements directly by its name.
 For example, look at the difference between these two
declarations:

30
Cont’d...
union anonymous union
struct { struct {
char title[50]; char title[50];
char author[50]; char author[50];
union { union {
float dollars; float dollars;
int yens; int yens;
} price; };
} book; } book;
31
Cont’d...
 The only difference between the two pieces of code is
that in the first one we gave a name to the union
(price) and in the second we did not.
 The difference is when accessing members dollars
and yens of an object.
 In the first case it would be:
book.price.dollars
book.price.yens

whereas in the second it would be:
book.dollars
book.yens
32
Enumerations (enum)
 Enumerations serve to create data types to contain
something different that is not limited to either
numerical or character constants nor to the constants
true and false.
 Its form is the following:
enum model_name {
value1,
value2,
value3,
. .
} object_name;
33
Cont’d...

For example, we could create a new type of variable
called color to store colors with the following
declaration:
enum colors_t {
black, blue, green, cyan, red, purple, yellow, white
};

For example, once declared the colors_t enumeration
in the following expressions will be valid:
colors_t mycolor;
mycolor = blue;
if (mycolor == green)
mycolor = red; 34
Cont’d...
 In fact our enumerated data type is compiled as an
integer and its possible values are any type of integer
constant specified.
 If it is not specified, the integer value equivalent to the
first possible value is 0 and the following ones follow
a +1 progression.
 Thus, in our data type colors_t that we defined before,
black would be equivalent to 0, blue would be
equivalent to 1, green to 2 and so on.

35
Cont’d...

If we explicitly specify an integer value for some of
the possible values of our enumerated type (for
example the first one) the following values will be the
increases of this,
for example:
enum months_t {
january=1, february, march, april, may, june, july, august,
september, october, november, december
}y2k;

In this case, variable y2k of the enumerated type months_t can
contain any of the 12 possible values that go from january to
december and that are equivalent to values between 1 and 12,
not between 0 and 11 since we have made january equal to 1.
36
Thank You !

37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy