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.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

C Language.
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Kernighan/Ritchie: Kelley/Pohl:
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Functions, Pointers, Structures Keerthi Nelaturu.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
ECE 103 Engineering Programming Chapter 53 Generic Algorithms Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
POINTERS.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Java Programming Language Lecture27- An Introduction.
ECE 103 Engineering Programming Chapter 45 Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Structures, Unions, Enumerations
Chapter 10-1: Structure.
DATA HANDLING.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 62 Stack Implementation
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Presentation transcript:

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 PSU ECE

Syllabus Nested Structures Structures and Functions Unions Examples

2 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.

3 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;

4 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.

5 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 */ };

6 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.

7 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.

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

9 Example: #include #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 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; }

10 Example: #include 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 = phase = Post: mag = phase =

11 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.

12 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; };

13 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.

14 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.

15 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 'A'

16 Example: union UData { int x; double y; char ch; }; struct UData unum; unum.x = 256; unum.y = -6.73; unum.ch = 'A'; unum 256 x y 'A' ch