Transcript union UData

ECE 103 Engineering Programming
Chapter 50
Structures Unions, Part 2
Herbert G. Mayer, PSU CS
Status 6/4/2014
Initial content copied verbatim from
ECE 103 material developed by
Professor Phillip Wong @ PSU ECE
Syllabus




Nested Structures
Structures and Functions
Unions
Examples
Nested Structures

A structure definition may include, among its
members, a structure variable of a different type.

To access a structure within a structure, use the
member operator ( . or -> ) multiple times.
2
Example:
typedef struct point2D /* Coordinates of a 2-D point */
{
double x, y;
} Point2D;
struct triangle /* Triangle's vertices are points */
{
Point2D vertex1, vertex2, vertex3;
};
struct triangle tri[3]; /* Array of three triangles */
struct triangle * ptr; /* Pointer to a triangle */
/* Initialize vertex1 of second triangle (index 1) */
tri[1].vertex1.x = 0; tri[1].vertex1.y = 0;
ptr = &tri[2]; /* Point to third triangle */
ptr->vertex1.x = 5;
3
Self-Referential Structures

It is illegal to define a structure with a member
variable of the same type as the structure.

It is perfectly legal to define a structure that contains
a pointer to the same type as itself.

These are called “self-referential structures” and are
used in data structures such as linked lists and trees.
4
Examples:
/* This is NOT legal in C */
struct list_node
{
int value;
struct list_node next; /* WRONG */
}
/* These are legal in C */
struct list_node
{
int value;
struct list_node * next; /* Uses pointer, so OK */
};
struct tree_node
{
int value;
struct tree_node * left, * right; /* Uses pointers, so OK
*/
};
5
Structures and Functions

Multiple values can be stored in a structure and
passed to a function. This reduces the number of
individual arguments that need passing.

Passing a structure to a function by value:



A copy of the structure variable’s contents is passed
to the function.
The function can change the copy but not the original
argument.
If the passed structure contains an array, a complete
copy of the array is passed.
6

Passing a structure to a function by reference:
 Pass by reference can be simulated by passing a
pointer to the structure as the function argument.
 It is more efficient to pass large structures to a
function by reference.
 If the pointed-to structure should not be changed,
then use the const modifier.

Multiple values can also be returned from a function
by storing them in a structure and returning the
structure.
7
Example:
typedef struct point2D
{
double x, y;
} Point2D;
void fun_v (Point2D p)
{
p.x = 10.5; p.y = 21.0;
}
void fun_p (Point2D * p)
{
p->x = 31.0; p->y = 500.0;
}
int main (void)
{
Point2D point;
point.x = 1.0; point.y = 5.0;
fun_v(point); /* After return from call: 1.0
fun_p(&point); /* After return from call: 31.0
return 0;
}
5.0
*/
500.0 */
8
Example:
#include <stdio.h>
#define MAXSIZE 100
typedef struct /* Encapsulate array data in a structure */
{
int maxsize; /* Maximum array size */
int numel;
/* Current number of elements */
int ele[MAXSIZE];
} array;
void DispArray (const array * p)
{
int k;
for (k = 0; k < p->numel; k++)
printf("%d\n", p->ele[k]);
}
int main (void)
{
array A1, A2;
A1.maxsize=MAXSIZE; A1.ele[0]=5; A1.ele[1]=-3; A1.numel=2;
A2 = A1;
DispArray(&A2);
return 0;
}
9
Example:
#include <stdio.h>
Actual Output:
Pre : mag = 0.000000
Post: mag = 6.403124
phase = 0.000000
phase = 0.896055
typedef struct /* Use this to hold multiple return values */
{
double mag, phase; /* Magnitude & phase of complex number */
} MagPhase;
MagPhase calculate_magphase (double a, double b)
{
MagPhase mp; /* Hold calculated values for return */
mp.mag
= sqrt(a*a + b*b);
mp.phase = atan2(b,a);
return mp;
}
int main (void)
{
MagPhase Q = {0, 0};
printf("Pre : mag = %f
phase = %f\n", Q.mag, Q.phase);
Q = calculate_magphase(4, 5);
printf("Post: mag = %f
phase = %f\n", Q.mag, Q.phase);
return 0;
}
10
Unions

Unions offer a way to economize on storage.

Like a structure, a union is a collection of variables of
different types.

However, only a single field can actually hold
information at any given time.
11

A union definition is similar to a structure definition.
Example:
/* Define a union */
union UData
{
char c_number;
int i_number;
float f_number;
double d_number;
};
12

To define a variable of type union UData:
union UData num;

To assign values to num:
num.c_number = 'A';
num.f_number = 3.14;
num.i_number = 2;
Assigning 3.14 to num cancels the original
assignment of 'A' to num.
Likewise, storing the integer value 2 in num will
cancel the assignment of 3.14.
13



For a union variable, the system allocates an amount
of storage large enough to hold its largest member.
This same storage is used for all the smaller union
members as well.
A member of a union can be:
 A variable or array of any legal data type
 A structure or another union

A union can be used to view a single piece of data as
different types, as specified in the union declaration.
14
Example:
struct SData
{
int x;
double y;
char ch;
};
struct SData snum;
snum.x = 256;
snum.y = -6.73;
snum.ch = 'A';
snum
x
256
y -6.73
ch 'A'
15
Example:
union UData
{
int x;
double y;
char ch;
};
struct UData unum;
unum.x = 256;
unum.y = -6.73;
unum.ch = 'A';
unum
x
y 256
-6.73
'A'
ch
16