Structures and UnionsCS-2301 B-term 20081 Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

Programming in C Chapter 10 Structures and Unions
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.
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.
Homework #4CS-2301 B-term Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Numerical Computation Review and Continuation CS-2301, B-Term Numerical Computation in C Review and Continuation CS-2301, System Programming for.
Arrays in C & C++CS-2303, C-Term Arrays in C & C++ (including a brief introduction to pointers) CS-2303 System Programming Concepts (Slides include.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
CS 536 Spring Run-time organization Lecture 19.
C and Data Structures Baojian Hua
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 C++ Syntax and Semantics, and the Program Development Process.
Learners Support Publications Classes and Objects.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
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.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Arrays in CCIS 1057 Fall Arrays in C (with a brief Introduction to Pointers) CIS 1057 Computer Programming in C Fall 2013 (Slides include materials.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Data Types Declarations Expressions Data storage C++ Basics.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
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.
+ 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.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
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.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
1 CS161 Introduction to Computer Science Topic #15.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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)
Dynamic Storage Allocation
Chapter 10-1: Structure.
Run-time organization
Advanced Programming Basics
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Classes and Objects.
Miscellaneous C++ Topics
Structures, Unions, and Typedefs
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chapter 9: Pointers and String
Structures, Unions, and Enumerations
Programming Fundamental
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language, 2 nd ed., by Kernighan and Ritchie and from C: How to Program, 5 th ed., by Deitel and Deitel)

Structures and UnionsCS-2301 B-term Reading Assignment Chapter 6 of Kernighan & Ritchie Note §6.9, Bit Fields. –These are used frequently in embedded systems –When you need to communicate directly with the bits of certain registers –Much about them is implementation dependent

Structures and UnionsCS-2301 B-term Structures and Unions The last major language facility in C to be introduced in this course. Essential for building up “interesting” data structures — e.g., Data structures of multiple values of different kinds Data structures of indeterminate size Essential for solving “interesting” problems Most of the “real” problems in the C world

Structures and UnionsCS-2301 B-term Definition — Structure A collection of one or more variables, typically of different types, grouped together under a single name for convenient handling Known as struct in C and C++

Structures and UnionsCS-2301 B-term struct Defines a type I.e., a new kind of data type that compiler regards as a unit E.g., struct motor { float volts; float amps; int phases; float rpm; }; //struct motor

Structures and UnionsCS-2301 B-term struct Defines a type E.g., struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Name of the type

Structures and UnionsCS-2301 B-term struct Defines a type E.g., struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Name of the type Note:– name of type is optional if you are just declaring a single struct (middle of p. 128)

Structures and UnionsCS-2301 B-term struct Defines a type E.g., struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Members of the struct

Structures and UnionsCS-2301 B-term Declaring struct variables struct motor p, q, r; Declares and sets aside storage for three variables – p, q, and r – of type motor struct motor M[25]; Declares a 25-element array of motor ; allocates 25 units of storage, each one big enough to hold the data of one motor struct motor *m; Declares a pointer to an object of type motor

Structures and UnionsCS-2301 B-term Accessing Members of a struct Let struct motor p; struct motor q[10]; Then p.volts — is the voltage p.amps — is the amperage p.phases — is the number of phases p.rpm — is the rotational speed q[i].volts — is the voltage of the i th motor q[i].rpm — is the speed of the i th motor

Structures and UnionsCS-2301 B-term Accessing Members of a struct (continued) Let struct motor *p; Then (*p).volts — is the voltage of the motor pointed to by p (*p).phases — is the number of phases of the motor pointed to by p

Structures and UnionsCS-2301 B-term Accessing Members of a struct (continued) Let struct motor *p; Then (*p).volts — is the voltage of the motor pointed to by p (*p).phases — is the number of phases of the motor pointed to by p Why the parentheses?

Structures and UnionsCS-2301 B-term Accessing Members of a struct (continued) Let struct motor *p; Then (*p).volts — is the voltage of the motor pointed to by p (*p).phases — is the number of phases of the motor pointed to by p Because ‘.’ operator has higher precedence than unary ‘*’

Structures and UnionsCS-2301 B-term Accessing Members of a struct (continued) Let struct motor *p; Then (*p).volts — is the voltage of the motor pointed to by p (*p).phases — is the number of phases of the motor pointed to by p Reason:– you really want the expression m.volt * m.amps to mean what you think it should mean!

Structures and UnionsCS-2301 B-term Accessing Members of a struct (continued) The (*p).member notation is a nuisance Clumsy to type; need to match ( ) Too many keystrokes This construct is so widely used that a special notation was invented, i.e., –p->member, where p is a pointer to the structure

Structures and UnionsCS-2301 B-term Previous Example Becomes … Let struct motor *p; Then p -> volts — is the voltage of the motor pointed to by p p -> phases — is the number of phases of the motor pointed to by p

Structures and UnionsCS-2301 B-term Operations on struct Copy/assign struct motor p, q; p = q; Get address struct motor p; struct motor *s s = &p; Access members p.volts; s -> amps;

Structures and UnionsCS-2301 B-term Operations on struct (continued) Notes:– –Passing an argument by value is an instance of copying or assignment –Passing a return value from a function to the caller is an instance of copying or assignment E.g,:– struct motor f(struct motor g) { struct motor h = g;...; return h; }

Structures and UnionsCS-2301 B-term Assigning to a struct (continued) K & R say (p. 131) –“If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure” I disagree:– –Copying is very fast on modern computers –Creating an object with malloc() and assigning a pointer is not as fast –Esp. if you want the object passed by value

Structures and UnionsCS-2301 B-term Initialization of a struct Let struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Then struct motor m = {208, 20, 3, 1800} initializes the struct See also p. 133 for initializing arrays of structs

Structures and UnionsCS-2301 B-term Initialization of a struct Let struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Then struct motor m = {208, 20, 3, 1800} initializes the struct See also p. 133 for initializing arrays of structs C99 introduces a new way of initializing a struct – truly ugly!

Structures and UnionsCS-2301 B-term Typedef Definition:– a typedef is a way of renaming a type –See §6.7 E.g., typedef struct motor Motor; Motor m, n; Motor *p, r[25]; Motor function(const Motor m; …);

Structures and UnionsCS-2301 B-term Typedef Definition:– a typedef is a way of renaming a type –See §6.7 E.g., typedef struct motor Motor; Motor m, n; Motor *p, r[25]; Motor function(const Motor m; …); I.e., after typedef, you don’t have to type the word “ struct ” any more!

Structures and UnionsCS-2301 B-term typedef (continued) typedef may be used to rename any type –Convenience in naming –Cleaner, more readable code –Portability across platforms E.g., –typedef char *String; E.g., –typedef int size_t; –typedef long int32; –typedef long long int64;

Structures and UnionsCS-2301 B-term typedef (continued) typedef may be used to rename any type –Convenience in naming –Cleaner, more readable code –Portability across platforms E.g., –typedef char *String; E.g., –typedef int size_t; –typedef long int32; –typedef long long int64; These three may change from platform to platform Defined once in a.h file!

Structures and UnionsCS-2301 B-term Unions A union is like a struct, but only one of its members is stored, not all I.e., a single variable may hold different types at different times Storage is enough to hold largest E.g., union { int ival; float fval; char *sval; } u;

Structures and UnionsCS-2301 B-term Unions (continued) It is programmer’s responsibility to keep track of which type is stored in a union at any given time! E.g., (p. 148) struct taggedItem { enum {iType, fType, cType} tag; union { int ival; float fval; char *sval; } u; };

Structures and UnionsCS-2301 B-term Unions (continued) It is programmer’s responsibility to keep track of which type is stored in a union at any given time! E.g., (p. 148) struct taggedItem { enum {iType, fType, cType} tag; union { int ival; float fval; char *sval; } u; }; Members of struct are:– enum tag; union u; Value of tag says which member of u to use

Structures and UnionsCS-2301 B-term Unions (continued) unions are used much less frequently than structs — mostly in the inner details of operating system in device drivers in embedded systems where you have to access registers defined by the hardware

Structures and UnionsCS-2301 B-term Why structs and unions ? Open-ended data structures –E.g., structures that may grow during processing –Avoids the need for realloc() and a lot of copying Self-referential data structures –Lists, trees, etc.

Structures and UnionsCS-2301 B-term Example struct item { char *s; item *next; }

Structures and UnionsCS-2301 B-term Example struct item { char *s; item *next; } I.e., an item can point to another item Yes! This is legal!

Structures and UnionsCS-2301 B-term Example struct item { char *s; item *next; } I.e., an item can point to another item … which can point to another item

Structures and UnionsCS-2301 B-term Example struct item { char *s; item *next; } I.e., an item can point to another item … which can point to another item … which can point to yet another item

Structures and UnionsCS-2301 B-term Example struct item { char *s; item *next; } I.e., an item can point to another item … which can point to another item … which can point to yet another item … etc.

Structures and UnionsCS-2301 B-term Example struct item { char *s; item *next; } I.e., an item can point to another item … which can point to another item … which can point to yet another item … etc. Thereby forming a list of items

Structures and UnionsCS-2301 B-term Example – Homework #4 Instead of justify returning an array of pointers to character arrays char **justify(const char *text, const int width, const int tab); It could instead return a pointer to a list of lines typedef struct lineItem { char *s; lineItem *next} line; line *justify(const char *text, const int width, const int tab);

Structures and UnionsCS-2301 B-term Example – Homework #4 (continued) No need for justify to realloc() the array of pointers when # of lines exceeds what was anticipated This is real reason why realloc() is rarely used! Same is true for ReadAndPrint() Could have created a list of textItems to feed to justify()

Structures and UnionsCS-2301 B-term Example – Homework #4 (continued) No need for justify to realloc() the array of pointers when # of lines exceeds what was anticipated This is real reason why realloc() is rarely used! Same is true for ReadAndPrint() Could have created a list of textItems to feed to justify() This is typical way of thinking in C!

Structures and UnionsCS-2301 B-term Questions? This material will be on Exam #2

Structures and UnionsCS-2301 B-term Happy Thanksgiving!