Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Similar documents
Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

ENCM 339 Fall 2017 Tutorial for Week 8

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

Kurt Schmidt. October 30, 2018

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

QUIZ. What are 3 differences between C and C++ const variables?

Slide Set 1 (corrected)

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Motivation was to facilitate development of systems software, especially OS development.

Slide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Slide Set 5. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Motivation was to facilitate development of systems software, especially OS development.

CS 61c: Great Ideas in Computer Architecture

CSE 333 Midterm Exam 2/14/14

Operator overloading

EL6483: Brief Overview of C Programming Language

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Fundamentals of Programming Session 4

CSE 333 Midterm Exam 5/10/13

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

ENCM 501 Winter 2019 Assignment 9

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

QUIZ. Source:

CS102: Variables and Expressions

C++ for Java Programmers

Goals of this Lecture

Introduction to C: Pointers

CSE 333 Midterm Exam Sample Solution 5/10/13

Slide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

CSCI 171 Chapter Outlines

CSE 333 Midterm Exam 5/9/14 Sample Solution

ENCM 335 Fall 2018 Tutorial for Week 13

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

CSE P 501 Exam 12/1/11

Chapter 1 Getting Started

211: Computer Architecture Summer 2016

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CIS 190: C/C++ Programming. Classes in C++

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CSE 333 Midterm Exam 7/29/13

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

ADTs & Classes. An introduction

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Here's how you declare a function that returns a pointer to a character:

CSE 333 Midterm Exam 7/22/12

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Have examined process Creating program Have developed program Written in C Source code

TDDE18 & 726G77. Functions

Object Oriented Design

Compiling and Running a C Program in Unix

Topic 6: A Quick Intro To C

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

Chapter 2, Part I Introduction to C Programming

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Exercise Session 2 Simon Gerber

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Slides for Lecture 15

PIC 10A Objects/Classes

Assignment #5 Answers

Lecture 12 Modular Programming with Functions

DECLARAING AND INITIALIZING POINTERS

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture 10: building large projects, beginning C++, C++ and structs

CIS 2107 Computer Systems and Low-Level Programming Fall 2010 Midterm

Data Representation and Storage

ENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16

Lectures 5-6: Introduction to C

Fundamental of Programming (C)

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Ch. 10: Name Control

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Ch. 3: The C in C++ - Continued -

CSE 333 Midterm Exam July 24, Name UW ID#

#1 #2 with corrections Monday, March 12 7:00pm to 8:30pm. Please do not write your U of C ID number on this cover page.

Programming. Structures, enums and unions

CS1102: What is a Programming Language?

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

QUIZ. What is wrong with this code that uses default arguments?

Transcription:

Slide Set 6 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 2/38 Contents Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 3/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 4/38 Introduction to structure types in C A variable of a structure type can contain multiple pieces of data, with different types allowed for the different pieces. The keyword struct is used to create a structure type, and sometimes structure types are called struct types. How is a structure type different from an array type?

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 5/38 As an example, a structure type could be used for information related to single student. What would be reasonable choices for type of storage in the table below? piece of data last name given names ID number type of storage Let s write some C code for a type that can hold a student s last name, given names, and ID number.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 6/38 Example structure variable declarations The code written in response to the previous slide created a type called struct StudentInfo. So what do each of the following variable declarations do? struct StudentInfo a; // line 1 struct StudentInfo *b; // line 2 struct StudentInfo c[100]; // line 3

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 7/38 Accessing members of structure variables The individual variables within a structure variable are called members. (Other words sometimes used for members are fields, or components.) A member can be accessed using the structure member operator, which is a dot. There is example code on the next slide. Let s draw the AR of main at point (1). (But let s simplify the pictures of the arrays of char so we don t have to draw 96 array elements!)

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 8/38 #include <string.h> #define directives and declaration of struct StudentInfo int main(void) { struct StudentInfo s; strcpy(s.last_name, "Coyote"); strcpy(s.given_names, "Wile E."); s.id_number = 123456; // (1) } return 0; Now let s rewrite main to get the same effect, using a thing called a structure initializer.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 9/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 10/38 Creating alternate names for types with typedef typedef declarations do not define new types. Instead they define aliases (alternate names) for existing types. The simplest syntax for typedef looks like this: typedef variable declaration (Note that the variable declaration includes a semicolon.) The new name for a type goes in the spot where the name of the variable would appear in a normal variable declaration. So, what do these lines of code mean? typedef int FOO; // line 1 typedef double *PTR2DBL; // line 2 typedef struct StudentInfo stu_info_t; // line 3

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 11/38 Creating aliases for structure type names in C is common, because it is annoying to have to key in the word struct many times. This is how I would set up the struct StudentInfo type, and then create an handy alternate name for the type... #define LAST_NAME_MAX_LENGTH 31 #define GIVEN_NAME_MAX_LENGTH 63 struct StudentInfo { char last_name[last_name_max_length + 1]; char given_names[given_name_max_length + 1]; int id_number; }; typedef struct StudentInfo stu_info_t;

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 12/38 On the previous slide, there is a clear separation between the creation of the type and and the creation of an alternate name for the type. It s possible to save even more keystrokes by merging the type declaration and the typedef into a single declaration... #define LAST_NAME_MAX_LENGTH 31 #define GIVEN_NAME_MAX_LENGTH 63 typedef struct StudentInfo { char last_name[last_name_max_length + 1]; char given_names[given_name_max_length + 1]; int id_number; } stu_info_t; The code above has exactly the same effect as the code on Slide 11.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 13/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 14/38 Operations allowed with structure types Let s define the term structure object to refer to variables and function arguments of structure types. (Later we ll extend the definition to include some other kinds of data related to structure types.) Here are some (not all) of the things you can do with a structure object: access members using the. operator take its address with the & operator copy it, one of three ways: assignment with =, passing a function argument, passing a function return value

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 15/38 And here are some things you can t do with structure objects: arithmetic with operators such as + - * / % ++ --, etc. comparison with any of ==!= < <= > >= In C++ you can use operator overloading to define meanings for the above operators when used with structure types. You can t do that in C.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 16/38 To make a simple example of copying structure objects, let s consider the example of representing vectors in the (x,y) plane, and writing a C function to compute the effect of rotating a vector by 90 degrees. y 90 degrees x What code do we need to declare a type called struct vec2d and an alias for that type called vec2d_t?

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 17/38 The formula for 90-degree (counterclockwise) rotation is simple. If vector w is the 90-degree rotation of vector v, then x component of w = (y component of v) y component of w = x component of v Let s write a function definition to correspond to this prototype: vec2d_t rotate90(vec2d_t vec); Then let s write a simple main function to call rotate90 and draw diagrams to show how arguments and return values get copied.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 18/38 Attention: Rules for structures are not the same as the rule for arrays! Arrays are potentially large objects that could be expensive to copy. Structure objects are also potentially large things that could be expensive to copy. Using an array name as a function argument results in copying only the address of element 0, a relatively small thing. Therefore, you might guess that using a structure object name as a function argument also just sends an address to the called function. That guess would be wrong. We ve just seen that using a structure object as a function argument results in copying the whole object.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 19/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 20/38 Pointers to structure objects These are used a lot in real-world C code. As with any other data type, the address of a struct object is the lowest address of all of the bytes allocated for the object. A pointer-to-structure contains a a single address only, the address of an entire structure object.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 21/38 Experience has shown me that it s very tempting to conclude that a pointer to a structure object that has, say, three members, must contain three different addresses. My answer to that is: No, no, no, really, No! Let s draw a diagram for an example: struct Foo { double d; int i; char c[4]; }; int main(void) { struct Foo f = { 2.25, 999, "ABC" }; struct Foo *p = &f; // (1) return 0; }

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 22/38 The -> operator This is an arrow made from minus and greater-than, with no space in between. If p is a pointer to a structure object, then p-> member name has exactly the same meaning as (*p). member name The parentheses above are important dot has higher precedence than star, so *p. member name is equivalent to *(p. member name ), which makes no sense if p is some kind of pointer.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 23/38 Let s look at an alternate way to implement the rotate90 function, with this prototype: void rotate90alt(vec2d_t *out, const vec2d_t *in); in points to the original vector, and out points to a vector that will get the result of rotating the original vector by 90 degrees. What should the function definition of rotate90alt be? Let s write a simple main function to call rotate90alt and draw diagrams to show how arguments are used.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 24/38 Pointers to data within structure objects It s permissible and often useful to have pointers point to members within structure objects, but these pointers must have appropriate types. Here is an example, which continues on the next slide... #include <string.h> struct table_entry { int number; char symbol[3]; double weight; }; typedef struct table_entry table_entry_t;

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 25/38 void titanium(table_entry_t *te); int main(void) { table_entry_t x; titanium(&x); return 0; } void titanium(table_entry_t *te) { te->number = 22; strcpy(te->symbol, "Ti"); te->weight = 47.90; } Let s make a diagram for the moment when the strcpy function starts work.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 26/38 Why use pointers to structures as function arguments? Answer: To save time and memory space. Remember that a pointer to a structure object is just a single address, the lowest of all the addresses of all the bytes used for the structure object. So a pointer typically occupies much less space in an AR than a structure object. And copying an address, which happens when a pointer is sent as a function argument, takes much less time than copying a large structure object.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 27/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 28/38 An earlier version of this example did not include the declaration and initialization of q. struct Foo { double d; int i; char c[4]; }; int main(void) { struct Foo f = { 2.25, 999, "ABC" }; struct Foo *p = &f; double *q = &f.d; // (1) return 0; } If we were told what address is in p at point (1), what could we say about the address in q at that same moment? So what is the difference between p and q?

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 29/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 30/38 Structures within structures It is common to declare a structure type in which one or more members have other structure types. Suppose in some program we would like to keep track of circles in an (x,y)-plane each circle has a center and a radius. y radius center x

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 31/38 Suppose that this code from earlier in this Slide Set is available... struct vec2d { double x; double y; }; typedef struct vec2d vec2d_t; How could we set up a structure type for circles, using a member of type vec2d_t to specify the center of a circle? Let s write a simple main function to demonstrate use of some variables of our new structure type.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 32/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 33/38 Include guards Here s a reminder of an important definition: A translation unit is the output the C preprocessor produces starting from a single.c file. So a translation unit may contain contents from one or more files included using #include, macro expansions based on #define directives, and so on. It s an error to repeat certain kinds of C code in a translation unit. Among these kinds of code are structure type declarations typedef declarations various other things

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 34/38 An include guard is a preprocessor trick used to avoid inadvertent repetition of code within a translation unit. Before we look at how to write an include guard, let s look at how things can go wrong without include guards. Suppose Coder A writes these two tiny header files: file vec2d.h struct vec2d { double x; double y; }; typedef struct vec2d vec2d_t; file circle.h #include "vec2d.h" struct circle { vec2d_t center; double radius; }; typedef struct circle circle_t; Then Coder A gives the header files to Coder B...

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 35/38 Coder B wants to write a program with some variables of type vec2d_t and some other variables of type circle_t, and writes something like this... file myprog.c #include <stdio.h> #include "vec2d.h" #include "circle.h" int main(void) { vec2d_t v; circle_t c; //... } Why will a C compiler reject Coder B s program? What would be a very simple fix for this problem?

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 36/38 It turns out that the simple fix offered in response to a question on the last slide does not scale it s not a reasonable solution for a project with many large header files, many of which #include some of the other header files. A solution that scales well is shown on the next slide. In each of the two header files, the first, second and last lines form what are known as include guards. Let s briefly explain why the include guards make the following sequence of directives safe and effective... #include "vec2d.h" #include "circle.h"

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 37/38 file vec2d.h #ifndef VEC2D_H #define VEC2D_H struct vec2d { double x; double y; }; typedef struct vec2d vec2d_t; #endif file circle.h #ifndef CIRCLE_H #define CIRCLE_H #include "vec2d.h" struct circle { vec2d_t center; double radius; }; typedef struct circle circle_t; #endif Attention: It s customary to make the name of the macro used in an include guard similar to the name of the header file it appears in for example, VEC2D_H is obviously derived from vec2d.h. That helps with readability, but is not mandatory the preprocessor does not care whether the macro name is similar to the file name.

ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 38/38 Final notes about include guards These are really common in practical C projects! For example, on a Linux system, the first non-comment two lines of <string.h> are #ifndef _STRING_H #define _STRING_H 1 and the last line is #endif /* string.h */