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.

Slides:



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

C Language.
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.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
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.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
C and Data Structures Baojian Hua
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
CS 201 Structure & Union Debzani Deb.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Computer Science 210 Computer Organization Pointers.
Programming in C Pointer Basics.
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.
Working with Structures. Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Learners Support Publications Classes and Objects.
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.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.
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.
chap11 Chapter 11 Structure and Union Types.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
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.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
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.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
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)
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Derived types.
Classes and Objects.
ECE 103 Engineering Programming Chapter 32 Array Parameters
Structure and Union Types
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
Programming in C Pointer Basics.
Chapter 11 Structure and Union Types.
Presentation transcript:

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 103 material developed by Professor Phillip PSU ECE

Syllabus Structure Definition Accessing a Structure Initializing a Structure Pointer to struct Examples

2 Structure Definitions Simple variables are declared with a single data type and can hold values only of that type. Example: /* Arrays for up to sixty students */ char Student_Name[60][100]; int ID_Number[60]; int HW_Scores[60][6]; double HW_average[60];

3 A structure allows multiple variables to be grouped together and be referenced as an aggregate variable. struct tag { /* Variable declarations listed here */ };  tag is the name assigned to the structure.  Braces { } delimit the body of the structure.  A semicolon is needed after the ending brace.  Variables are declared within the structure body. Each variable is a member of the structure.  Structures can have either block or file scope.

4 Example: struct Student_Info { char Student_Name[100]; int ID_Number; int HW_Scores[6]; float HW_Average; }; Note: This only defines the “layout” of the structure. It does not actually allocate storage for the member variables.

5 Once the structure is defined, a variable can be declared to be of that structure type. Example: Declare a structure variable for a single student: struct Student_Info SData; Student_Name ID_Number HW_Scores SData HW_Average … When SData is declared, storage is now allocated to hold the structure’s member variables.

6 Example: Declare an array of structures for sixty students: struct Student_Info SData[60]; Student_Name ID_Number HW_Scores SData[0] HW_Average … Student_Name ID_Number HW_Scores SData[1] HW_Average … Student_Name ID_Number HW_Scores SData[59] HW_Average …......

7 Defining a structure can be combined with declaring a variable of that structure type. Example: struct point3D { double x, y, z; int color; } point[1000];

8 The structure tag can be omitted if a named structure is not required. Example: struct { double x, y, z; int color; } point[1000]; Note: If there is no structure tag, it cannot be used for later variable declarations.

9 A typedef can alias away the “struct” part. Example: /* This has separate structure and typedef definitions */ struct point3D { double x, y, z; int color; }; typedef struct point3D Point3D; Point3D pt; Example: /* This combines the structure and typedef definitions */ typedef struct point3D { double x, y, z; int color; } Point3D; Point3D pt;

10 If a typedef is combined with a structure definition, the tag is optional. Example: typedef struct { double x, y, z; int color; } Point3D; /* Variable declarations */ Point3D pt; Note: If the tag is omitted, then structure variable declarations must also omit the struct keyword. Example: struct Point3D pt; /* Causes compiler error */

11 Accessing a Structure To access a member of a structure variable, use the member operator (a period) : struct_name.member_varname  struct_name is the name of the structure.  member_varname is the name of the member variable within the structure. To access an array of structures: struct_varname[index].member_varname

12 Example: /* Structure definition */ struct window { int win_id; int win_ulc_x, win_ulc_y; int win_width, win_height; int win_color; char win_title[80]; }; /* Variable declarations */ struct window w; struct window win[10]; /* Change the values of some member variables */ w.win_id = 5; w.win_id++; strcpy(w.win_title, "Main_Window"); win[3].win_width = 300; win[3].win_height = 250;

13 Example: #include #define NAMELEN 80 #define NUM 3 typedef struct ID_info { char FirstName[NAMELEN]; int Age; } ID_info_t; int main (void) { ID_info_t friend[NUM]; int k; for (k = 0; k < NUM; k++) { printf("Enter first name: "); scanf("%s", friend[k].FirstName); printf("Enter age: "); scanf("%d", &friend[k].Age); } for (k = 0; k < NUM; k++) { printf("%s %d\n", friend[k].FirstName, friend[k].Age); } return 0; } Actual Output: Enter first name: Joe Enter age: 21 Enter first name: Sara Enter age: 19 Enter first name: Frank Enter age: 20 Joe 21 Sara 19 Frank 20

14 Initializing Structures A structure variable can be initialized when it is declared. Initial values for the members are enclosed in braces. Example: struct point3D { double x, y, z; int color; }; struct point3D pt = {0, 30, 15, 255};

15 Example: struct point3D { double x, y, z; int color; }; struct point3D pt[3] = {{ 0, 30, 15, 255}, {10, 25, 15, 128}, { 0, 30, 0, 16}};

16 Designated Initializers for Structures When a structure variable is declared, C99 allows the members of the structure to be initialized by name, if desired. /* C90 version */ #include struct data { int x; int v[2]; }; int main (void) { struct data x = {1, {5, 2}}; return 0; } // C99 version #include struct data { int x; int v[2]; }; int main (void) { struct data P = {.x=1,.v[0] = 5,.v[1] = 2}; return 0; } C99

17 Assignment Operator Applied to Structures Given two structure variables of the same type, one variable can be copied directly to the other using the assignment operator ( = ). When copying a structure, the value of each member variable is copied, including entire arrays. If structures can be assigned to each other, can they also be compared using ==, >, <, etc? Unfortunately, no. This is not legal in C.

18 Example: struct point3D { double x, y, z; int color; }; typedef struct point3D Point3D; Point3D pt1, pt2, pt3; /* Initialize pt1 */ pt1.x = 10; pt1.y = 20; pt1.z = 0; pt1.color = 34; /* Copy member variables individually */ pt2.x = pt1.x; pt2.y = pt1.y; pt2.z = pt1.z; pt2.color = pt1.color; /* Copy entire structure at once */ pt3 = pt1;

19 Example: #include #define SIZE 3 typedef struct { int x[SIZE]; } array; int main (void) { array P = {{1,4,6}}, Q = {{0}}; int k; for (k = 0; k < SIZE; k++) printf("P.x[%d] = %d Q.x[%d] = %d\n", k, P.x[k], k, Q.x[k]); Q = P; /* Copy entire array by copying structure */ printf("\n"); for (k = 0; k < SIZE; k++) printf("P.x[%d] = %d Q.x[%d] = %d\n", k, P.x[k], k, Q.x[k]); return 0; } Actual Output: P.x[0] = 1 Q.x[0] = 0 P.x[1] = 4 Q.x[1] = 0 P.x[2] = 6 Q.x[2] = 0 P.x[0] = 1 Q.x[0] = 1 P.x[1] = 4 Q.x[1] = 4 P.x[2] = 6 Q.x[2] = 6

20 Pointers to Structures Variables that point to structures can be declared: struct tag * var_name; To dereference a structure pointer, parentheses are needed, since the member operator has higher precedence than the indirection operator.  *svar.mvar is incorrectly parsed as *(svar.mvar).  (*svar).mvar is the correct dereferencing syntax.

21 Example: typedef struct point2D { int x, y; } Point2D; Point2D point = {0, 0}; Point2D * ptr = &point; /* Access member variable via pointer */ (*ptr).x = 1; (*ptr).y = 5;

22 Because the syntax (*ptr).x is clumsy, an alternative syntax is available: pointer_varname->member_varname  -> is the pointer operator, which consists of a hyphen followed by a greater-than sign.  No whitespace is permitted between – and >

23 Example: Point2D point; Point2D * ptr; point.x = 10; printf("%d\n", point.x); ptr = &point; (*ptr).x = 10; /* Assign 10 to member x */ printf("%d\n", (*ptr).x); ptr->x = 10;/* This does same thing */ printf("%d\n", ptr->x);

24 Example: #include #define NAMELEN 80 #define NUM 3 typedef struct ID_info { char FirstName[NAMELEN]; int Age; } ID_info_t; int main (void) { ID_info_t friend[NUM], * f = friend; /* f is pointer to friend */ int k; for (k = 0; k < NUM; k++) { printf("Enter first name: "); scanf("%s", (f+k)- >FirstName); printf("Enter age: "); scanf("%d", &(f+k)->Age); } for (k = 0; k < NUM; k++) { /* Show access using both pointer and array notation */ printf("%s %d\n", (f+k)->FirstName, f[k].Age); } return 0; } Actual Output: Enter first name: Joe Enter age: 21 Enter first name: Sara Enter age: 19 Enter first name: Frank Enter age: 20 Joe 21 Sara 19 Frank 20