17
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

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

Embed Size (px)

Citation preview

Page 1: 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

ECE 103 Engineering ProgrammingChapter 50

Structures Unions, Part 2

Herbert G. Mayer, PSU CSStatus 6/4/2014

Initial content copied verbatim fromECE 103 material developed by

Professor Phillip Wong @ PSU ECE

Page 2: 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

Syllabus Nested Structures Structures and Functions Unions Examples

Page 3: 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

3

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.

Page 4: 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

4

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;

Page 5: 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

5

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.

Page 6: 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

6

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

*/};

Page 7: 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

7

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.

Page 8: 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

8

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.

Page 9: 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

9

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 5.0 */fun_p(&point); /* After return from call: 31.0 500.0 */return 0;

}

Page 10: 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

10

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;

}

Page 11: 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

11

Example:#include <stdio.h>

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;

}

Actual Output:Pre : mag = 0.000000 phase = 0.000000Post: mag = 6.403124 phase = 0.896055

Page 12: 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

12

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.

Page 13: 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

13

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;

};

Page 14: 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

14

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.

Page 15: 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

15

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.

Page 16: 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

16

Example:

struct SData

{

int x;

double y;

char ch;

};

struct SData snum;

snum.x = 256;

snum.y = -6.73;

snum.ch = 'A';

x

y

ch

snum

256

-6.73

'A'

Page 17: 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

17

Example:

union UData

{

int x;

double y;

char ch;

};

struct UData unum;

unum.x = 256;

unum.y = -6.73;

unum.ch = 'A';

unum

256xy -6.73'A'ch