Вы находитесь на странице: 1из 8

Lect.eng.

Adriana ALBU, PhD


Politehnica University of Timioara

March 2014
1

Chapter 11
User-defined types addendum

This is an addendum to Chapter 11. It presents one more way to create user-
defined types (the union).

11-a.1 The union
A union is a storage space that is shared by two or more variables (usually of
different types) at different moments [BH92]. It is typically used when an ele-
ment can be one, but only one of many things at a time. Its definition is similar
to that of a structure, but uses the keyword uni on:
uni on uni on_name{
t ype1 var 11, var 12;
t ype2 var 2;

t ypen var n;
} l i st _of _var i abl es;
The following example is a union that contains two members, an integer n and
a float x. A variable, number , is declared using this new data type:
uni on exampl e{
i nt n;
f l oat x;
}number ;
A union variable can be also declared in a separated instruction, once the data
type was defined. The visibility rules are the same: the data type should be
available inside the code sequence that contains the variables declaration:
uni on exampl e anot her Number ;
The members of a union are accessed through the same operators that are used
for a structure. The operator dot (. ) is used to access the members of a union
variable (for instance number . n=7; ), while the operator arrow (- >) should
Computer Programming The C Language

2

March 2014
Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

be used when the members are accessed through a pointer. The following func-
tion receives as argument a pointer to a union variable and assigns the value 7
to its integer member:
voi d aFunct i on( uni on exampl e *pNumber ) {
pNumber - >n=7;
}
When a union variable (for instance number ) is declared, it will be allocated
enough memory space to store the largest element of the union. Here is the
difference between a structure and a union: the members number . n and num-
ber . x occupy the same memory location, having, naturally, the same memory
address. This means that writing into one will overwrite the other.
There is no way to determine the data type of the variable that is stored by a
union at a time. It is the programmers responsibility to take care about the
content (from data type point of view) of a union variable; the type retrieved
should be the type most recently stored [DL06] [BK03].
Program 11-a.1.1 is a simple example that uses the previously declared union in
order to demonstrate that the members have all the same memory address. The
%X from pr i nt f s format descriptor is used to show the hexadecimal value of
an address. The program will display the messages:
The addr ess of member n: FFF2
The addr ess of member x: FFF2
Though the address of member variables (FFF2 in this example) can be differ-
ent each time the program is run, it will be the same for all members at a mo-
ment.
Program 11-a.1.1
#i ncl ude <st di o. h>
uni on exampl e{
i nt n;
f l oat x;
};
voi d mai n( voi d) {
uni on exampl e number ;
pr i nt f ( " The addr ess of member n: %X" , &number . n) ;
11 User-defined types addendum

Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

March 2014
3

pr i nt f ( " \ nThe addr ess of member x: %X" , &number . x) ;
get ch( ) ;
}
As could be seen in the previous examples, the unions admit the same opera-
tions as the structures do: accessing a member, assignment, taking the address.
Regarding the initialization, a union may only be initialized with a value of the
type of its first member [BK03]; therefore, the union exampl e, considered
here, can only receive an integer value when it is initialized.
Unions may contain structures and arrays and also may occur within structures
and arrays. If a member variable of this kind of nested elements should be
accessed, the notation is identical to that for nested structures. The following
example declares an array of nested elements; each location of the array is a
structure that contains several variables, including a union.
st r uct {
char name[ 20] ; / / and somet hi ng el se i f necessar y
i nt uni onType; / / 0 means mar k and 1 means gr ade
uni on {
i nt mar k; / / f or i nst ance 7
char gr ade; / / f or i nst ance ' A'
} r esul t ;
} st udent s[ 50] ;
The array contains information about students; this is stored in a structure: the
name (and other possible data about the student) and also the r esul t of
students examination. This r esul t can be a mar k or a gr ade and it is
stored within a union. In order to know which variable of the union is used for
each student, an integer (uni onType) is set to 0 for mark and to 1 for grade.
Program 11-a.1.2 presents the entire example.
Program 11-a.1.2
#i ncl ude <st di o. h>
st r uct {
char name[ 20] ;
i nt uni onType;
Computer Programming The C Language

4

March 2014
Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

uni on{
i nt mar k;
char gr ade;
} r esul t ;
} st udent s[ 50] ;
i nt n;

voi d r ead( voi d) {
i nt i ;
pr i nt f ( " Number of st udent s: " ) ;
scanf ( " %d" , &n) ;
f or ( i =0; i <n; i ++) {
pr i nt f ( " \ nSt udent %d: \ n" , i +1) ;
pr i nt f ( " Name: " ) ;
scanf ( " %s" , st udent s[ i ] . name) ;
do{
pr i nt f ( " Mar k ( 0) or gr ade ( 1) ? " ) ;
scanf ( " %d" , &st udent s[ i ] . uni onType) ;
}whi l e( st udent s[ i ] . uni onType! =0 &&
st udent s[ i ] . uni onType! =1) ;
i f ( st udent s[ i ] . uni onType==0) {
pr i nt f ( " Mar k: " ) ;
scanf ( " %d" , &st udent s[ i ] . r esul t . mar k) ;
}
el se{
pr i nt f ( " Gr ade: " ) ;
st udent s[ i ] . r esul t . gr ade=get ch( ) ;
}
}
}

voi d wr i t e( voi d) {
i nt i ;
f or ( i =0; i <n; i ++) {
pr i nt f ( " \ n%s " , st udent s[ i ] . name) ;
i f ( st udent s[ i ] . uni onType==0)
pr i nt f ( " mar k %d" , st udent s[ i ] . r esul t . mar k) ;
el se
pr i nt f ( " gr ade %c" , st udent s[ i ] . r esul t . gr ade) ;
11 User-defined types addendum

Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

March 2014
5

}
}

voi d mai n( voi d) {
r ead( ) ;
wr i t e( ) ;
get ch( ) ;
}

11-a.2 Questions and exercises
A. Find the error.
1. uni on exampl e{
i nt n;
f l oat x;
}number ;
number . n=7;
number . x=9. 75;
pr i nt f ( " %d" , number . n) ;
2. uni on exampl e{
i nt n;
f l oat x;
}number ={9. 75};
pr i nt f ( " %f " , number . x) ;
3. uni on exampl e{
i nt n;
f l oat x;
}number , *p;
number . n=7;
p=&number ;
pr i nt f ( " %d" , p. n) ;
B. Considering the following programs, specify what will be printed on the
screen once these programs are executed.
4. uni on exampl e{
i nt n;
f l oat x;
Computer Programming The C Language

6

March 2014
Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

}number ={7};
uni on exampl e anot her Number ;
anot her Number . x=9. 75;
pr i nt f ( " %d" , number . n) ;
5. uni on exampl e{
i nt n;
f l oat x;
}number , *p;
number . n=7;
p=&number ;
pr i nt f ( " %d" , p- >n) ;
C. Choose the correct answer (one only).
6. Which member of the following union determines the dimension of the
memory space allocated for the variable number ?
uni on exampl e{
i nt n;
f l oat x;
}number ;
a) n; b) x; c) n and x; d) the memory space allocated doesnt
have connection with the members; e) all answers are wrong.
7. Which of the following elements can be used to create a user-defined
type?
a) st r uct ; b) enum; c) uni on; d) all of them; e) none of
them.
8. Which user-defined type can be used to create a memory space that is
shared by two or more variables?
a) st r uct ; b) enum; c) uni on; d) all of them; e) none of
them.
9. What will be printed on the screen once the following sequence of code
is executed:
uni on exampl e{
i nt n;
11 User-defined types addendum

Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

March 2014
7

f l oat x;
}number ={7};
uni on exampl e anot her Number ;
anot her Number . x=9. 75;
anot her Number . n=17;
pr i nt f ( " %d" , number . n) ;
a) 7; b) 17; c) 9. 75; d) nothing; e) there is an error within
the union.
10. How can be accessed the mark of the first student from the following
vector?
st r uct {
char name[ 20] ;
i nt uni onType;
uni on {
i nt mar k;
char gr ade;
} r esul t ;
} st udent s[ 50] ;
a) st udent s. r esul t . mar k; b) uni on. r esul t . mar k;
c) st udent s[ 0] . mar k; d) st udent s[ 0] . r esul t . mar k;
e) all answers are wrong.
D. Write a program to solve the following problem.
11. Several children will spend their holiday abroad. The information that is
stored about them contains:
a. the childs name, which is a string;
b. the childs age, which is an integer and will determine if the
child is alone or he/she is accompanied by one of his/her parents;
c. if the child is less than 18 years old, then the name of a parent
must be specified; else, the amount of money that the child needs
is registered.
The program must display a list with all persons involved (children and
parents where is necessary) and must calculate the entire amount of
money that children less than 18 years old have with them.
Suggestion: A vector of the following data type could be used:
Computer Programming The C Language

8

March 2014
Lect.eng. Adriana ALBU, PhD
Politehnica University of Timioara

st r uct chi l d{
char chi l dName[ 20] ;
i nt chi l dAge;
uni on{
char par ent Name[ 20] ;
i nt money;
}addi t i onal I nf o;
};

11-a.3 Answers to questions and exercises
A. 1. This is not a compilation error, but the program will not have the desired
functionality. The member variables n and x share the same memory space.
The assignment number . x=9. 75 overwrites the value of number . n, there-
fore the value that is displayed has no meaning. 2. The union exampl e, can
only receive an integer value when it is initialized (because a union may only be
initialized with a value of the type of its first member). 3. If a union is accessed
through a pointer, then the operator - > should be used. The correct printing
function is: pr i nt f ( " %d" , p- >n) ; .
B. 4. 7. 5. 7.
C. 6. b) - because the largest element of the union is the f l oat one. 7. d). 8. c).
9. a). 10. d)

Вам также может понравиться