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

Similar documents
Slide Set 3. 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 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 1. 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

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

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

Slide Set 1 (corrected)

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

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 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Integer Multiplication and Division

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

CS102: Variables and Expressions

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

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

T02 Tutorial Slides for Week 2

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

Fundamental of Programming (C)

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

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

Work relative to other classes

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

Contents Slide Set 9. Final Notes on Textbook Chapter 7. Outline of Slide Set 9. More about skipped sections in Chapter 7. Outline of Slide Set 9

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Lecture 3. More About C

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

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

Variables and literals

THE FUNDAMENTAL DATA TYPES

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Goals of this Lecture

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Course Outline Introduction to C-Programming

Slide Set 15 (Complete)

More about Binary 9/6/2016

Chapter 3. Fundamental Data Types

ENCM 369 Winter 2019 Lab 6 for the Week of February 25

Introduction to C. Systems Programming Concepts

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Computers Programming Course 5. Iulian Năstac

Structures, Operators

Fundamentals of Programming

Programming for Engineers Introduction to C

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 12 Variables and Operators

Chapter 7. Basic Types

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Fundamentals of Programming Session 4

DECLARAING AND INITIALIZING POINTERS

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Reserved Words and Identifiers

Bits, Words, and Integers

Data Types. Data Types. Integer Types. Signed Integers

Introduction to C Language

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

ENCM 501 Winter 2015 Tutorial for Week 5

A flow chart is a graphical or symbolic representation of a process.

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

UNIT- 3 Introduction to C++

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

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

Display Input and Output (I/O)

Operators in C. Staff Incharge: S.Sasirekha

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

printf( Please enter another number: ); scanf( %d, &num2);

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Slides for Lecture 15

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Slides for Lecture 15

ENCM 335 Fall 2018 Tutorial for Week 13

Data types, variables, constants

Data Types and Variables in C language

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Full file at

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computers Programming Course 6. Iulian Năstac

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

3. Types of Algorithmic and Program Instructions

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Topic Notes: Bits and Bytes and Numbers

Declaration and Memory

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Arithmetic Expressions in C

Chapter 1 & 2 Introduction to C Language

Transcription:

Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018

ENCM 335 Fall 2018 Slide Set 2 slide 2/56 Contents An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 3/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 4/56 An example about types, expressions, and assignment in C The next few slides will step through some of the details of what this C code fragment would do on a typical computer of 2018... // Use of = in variable declarations is called // initialization. int i = 5; double x = -1.25, y = 0.0; // Use of = in a statement is called assignment. y = i + x; It s too bad that = is used for both initialization and assignment. Let s make a note about the difference between initialization and assignment.

ENCM 335 Fall 2018 Slide Set 2 slide 5/56 Types in the example In most modern implementations of C, the int type uses 32-bit two s-complement representation. See ENEL 353 for details of two s-complement systems! In most modern implementations of C, the double type uses a 64-bit representation that follows IEEE Standard 754. Do not worry about the details of that it s an ENCM 369 topic! It s unfortunate but necessary to use language like in most modern implementations... C standards allow some flexibility about how numbers are represented with patterns of zeros and ones.

ENCM 335 Fall 2018 Slide Set 2 slide 6/56 Effects of initialization The int variable i gets initialized to 5. 32 nodes in a circuit get voltages updated to represent this bit pattern... 00000000 00000000 00000000 00000101... which should make sense if you are up-to-date in ENEL 353. double variables x and y, initialized to 1.25 and 0.0, result in these bit patterns... 10111111 11110100 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 (You will not be tested on the details of number representations in ENCM 335.)

ENCM 335 Fall 2018 Slide Set 2 slide 7/56 What computer processors do Very roughly, all actions taken by computer processors fall into one of three categories: Copy a bit pattern from one place to another. Compute a new bit pattern, using one or two existing bit patterns as inputs. Decide what to do next this is needed for things like if statements, loops, and function call and return. The statement y = i + x; requires two compute steps, and at least one important copy step. (The exact number of copy steps depends on a lot of factors we don t need to know about in ENCM 335!)

ENCM 335 Fall 2018 Slide Set 2 slide 8/56 First compute step The expression i + x has operands of different types. The rule in C, similar to rules in most other programming languages, requires conversion of the int operand i to type double. A circuit takes the int representation of 5, which is 00000000 00000000 00000000 00000101 and generates the double representation of 5.0, which is 01000000 00010100 00000000 00000000 00000000 00000000 00000000 00000000

ENCM 335 Fall 2018 Slide Set 2 slide 9/56 Second compute step The bit patterns for 5.0 and 1.25, which are and 01000000 00010100 00000000 00000000 00000000 00000000 00000000 00000000 10111111 11110100 00000000 00000000 00000000 00000000 00000000 00000000 are fed as inputs into a circuit called a floating-point adder, which generates the double representation of 3.75 as output: 01000000 00001110 00000000 00000000 00000000 00000000 00000000 00000000

ENCM 335 Fall 2018 Slide Set 2 slide 10/56 Essential copy step The output of the floating-point adder, which is 01000000 00001110 00000000 00000000 00000000 00000000 00000000 00000000 gets copied into the storage location for y, replacing the all-zero bit pattern that represented 0.0 with the bit pattern that represents 3.75. This completes the assignment to the variable y.

ENCM 335 Fall 2018 Slide Set 2 slide 11/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 12/56 Expressions At this point it would be useful to provide a rough definition for the term expression. An expression is an identifier (the name of a variable, parameter or function), or a constant, or a meaningful chunk of C built using identifiers, constants, and/or operators. Consider the statement Let s list y = -x + 7 * f(z); expressions within the statement that are identifiers; expressions that are constants; all the more complex expressions.

ENCM 335 Fall 2018 Slide Set 2 slide 13/56 Types and values for expressions Every expression has a specific type. The type of a C expression is determined when a program is compiled, not when a program is run. (In dynamically typed languages such as Python, types of expressions often are determined at run-time.) C expressions that are not constants usually have values that are computed as a program runs.

ENCM 335 Fall 2018 Slide Set 2 slide 14/56 Examples of operator precedence and associativity Rules about operator precedence and associativity play important roles in determining how values of expressions are computed. In the following C fragment, what values do foo and bar get? What roles do precedence and associativity play? int i = 13, j = 5, k = 2, foo, bar; foo = i + j * k; bar = i - j - k;

ENCM 335 Fall 2018 Slide Set 2 slide 15/56 A near-complete list of C operators The next few slides list most of the C operators, in order of highest to lowest precedence. Many C operators have the same meanings that they have in Processing. Some may be unfamiliar to you they re unique to C and C++. We ll learn most of these operators as the course progresses, so please don t try to learn all of them right away!

ENCM 335 Fall 2018 Slide Set 2 slide 16/56 Here is a table of the highest three levels: precedence operators associativity 1 ( ) [ ]. -> left-to-right postfix ++ postfix -- 2 prefix ++ prefix -- right-to-left! ~ sizeof unary versions of + - * & 3 cast operator: ( type name ) right-to-left Let s make a few notes.

ENCM 335 Fall 2018 Slide Set 2 slide 17/56 The next ten levels are filled with binary operators. A binary operator sits in between its two operands. prec. operators associativity 4 multiplicative operators: * / % left-to-right 5 additive operators: + - left-to-right 6 shift operators: << >> left-to-right 7 relational operators: < <= > >= left-to-right 8 equality operators: ==!= left-to-right 9 bitwise AND: & left-to-right 10 bitwise XOR: ^ left-to-right 11 bitwise OR: left-to-right 12 logical AND: && left-to-right 13 logical OR: left-to-right

ENCM 335 Fall 2018 Slide Set 2 slide 18/56 Here are the lowest three levels: precedence operators associativity 14 conditional operator:? : right-to-left 15 = += -= *= /= %= right-to-left &= ^= = <<= >>= 16 comma operator:, left-to-right Let s make a few notes and consider some examples.

ENCM 335 Fall 2018 Slide Set 2 slide 19/56 Example use of the logical AND operator Let s look at the program on the next page and determine the output, making careful notes about how the expression y >= 0 && y <= 10 - x is evaluated.

ENCM 335 Fall 2018 Slide Set 2 slide 20/56 #include <stdio.h> void foo(int x, int y); int main(void) { foo(7, -2); foo(8, 3); foo(2, 4); return 0; } void foo(int x, int y) { if (y >= 0 && y <= 10 - x) printf("yep\n"); else printf("nope\n"); }

ENCM 335 Fall 2018 Slide Set 2 slide 21/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 22/56 The int and char types are both integer types In mathematics, the integers are all of the members of this infinite set: {..., 2, 1, 0, 1, 2, 3,... }. (In mathematics, numbers such as 0.5, 2, and π are real numbers that are not integers.) C has a large collection of integer types, and has a bunch of messy and complicated rules regarding these types. In contrast to the set of integers in mathematics, the range of values for a C integer type is finite. For any given integer type, there s a minimum value, and there s a maximum value. Probably the two most frequently used integer types in C are int and char.

ENCM 335 Fall 2018 Slide Set 2 slide 23/56 Really, char is an integer type! It s common to think of a char variable as a container for something like a letter (A Z, a z), a digit (0 9), a punctuation mark, or a space, etc. However, to understand how C programs work, it s extremely useful to know that char is an integer type, with a relatively small range of values. The range of values for char is not the same for every C development system. The most common range is { 128, 127,..., 126, 127 } ; this is the default for C programming on Linux, macos, and Windows. The second-most common range is { 0, 1, 2,..., 254, 255 }.

ENCM 335 Fall 2018 Slide Set 2 slide 24/56 char variables are often used to hold character codes For example, a very common character set is ASCII the American Standard Code for Information Interchange, in which character codes 48 to 57 stand for digits 0 to 9, in that order; character codes 65 to 90 stand for letters A to Z, in that order; character codes 97 to 122 stand for letters a to z, in that order; there are various other character codes for spaces, tabs, newlines, punctuation, and so on.

ENCM 335 Fall 2018 Slide Set 2 slide 25/56 Most current computers use extensions of the ASCII character set ASCII is supported, along with some kind of encoding for characters that don t appear in North American English. Here s a demonstration: #include <stdio.h> int main(void) { int i; printf("c program says... "); for (i = 65; i < 68; i++) printf("%d %c ", i, i); printf("... bye from C!\n"); return 0; } What s going on with %d and %c here, and what will the program output be?

ENCM 335 Fall 2018 Slide Set 2 slide 26/56 Character constants These begin and end with, the single quote, character, also known as forward quote or apostrophe. Each character constant is really a convenient symbol for a number a specific code for a single character. Here are a few examples, which assume that the ASCII character set is supported: constant meaning value a code for letter a 97 B code for letter B 66 3 code for digit 3 51 { code for left brace 123 \n code for newline 10 \\ code for backslash 92

ENCM 335 Fall 2018 Slide Set 2 slide 27/56 Important: Do not mix up single quotes and double quotes! Double quotes are used for string constants, things like "hello", "h", and "". (Another term for string constant is string literal.) In C, a single-character string constant, say, "x", does not mean the same thing as the similar-looking character constant x. (But in the Python programming language, "x" and x do mean exactly the same thing!) Finally, don t get mixed up with the ( backtick ) character. You can t make a C character constant with a backtick at either end.

ENCM 335 Fall 2018 Slide Set 2 slide 28/56 Conversions between char and int In most expressions involving one or more char values, the char value(s) gets converted to int before any arithmetic or comparison takes place. Example 1. Let s describe in detail how the assignment statement works in this code fragment: char a = G, b; int c = 3; b = a + c; Example 2. Let s explain how the comparison works in this code fragment, then write out the output... char x; for (x = 48; x < 58; x++) printf("%c", x);

ENCM 335 Fall 2018 Slide Set 2 slide 29/56 Because arithmetic and comparisons involving char values are really done using int values, functions that receive character codes as parameters often have int parameters, not char parameters. Example: Write a definition for isdigit. This function has one parameter, called c, and its return value indicates whether or not c is a character code for a digit.

ENCM 335 Fall 2018 Slide Set 2 slide 30/56 Connections to ENEL 353 material Right about now, students in ENEL 353 are learning about unsigned integer systems and two s complement signed integer systems. Fact: In C on most current laptop, desktop, and server computers, the char type uses 8-bit two s complement numbers; the int type uses 32-bit two s complement numbers.

ENCM 335 Fall 2018 Slide Set 2 slide 31/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 32/56 Introduction to Pointers The upcoming major topics in ENCM 335 are pointer types and the relationships between arrays and pointers. These topics are extremely important. You can t be an effective C programmer if you don t know them well. You also can t get a good grade in ENCM 335 if you don t know them well. The rest of Slide Set 2 will provide an introduction to pointer types. Slide Set 3 will show how pointers and arrays are related.

ENCM 335 Fall 2018 Slide Set 2 slide 33/56 Computer memory bits and bytes One of the key uses for main memory (the RAM circuits, not the hard drive) in a computer system is to hold data (such as variables and function parameters) belonging to running programs. The value of a bit is either 0 or 1. In a single-bit memory cell, a voltage close to ground represents a bit value of 0, and a somewhat higher voltage represents a bit value of 1. In memory systems, bits are grouped together to form bytes. In modern systems, there are 8 bits per byte; in other words, the width of a byte is 8 bits. Let s write some examples of possible values for a byte, using the ENEL 353 notation for binary numbers.

ENCM 335 Fall 2018 Slide Set 2 slide 34/56 Memory can be modeled as a giant array of bytes (Reminder: A model is a simplified description of a natural or engineered system; a good model helps to understand and perhaps to predict the behaviour of a system.) Each byte of memory has its own unique address, which is simply a number. The address of a byte indicates where the byte is located, not what the value of the byte is. The address space of a computer is the set of all possible addresses. Let s sketch the address space of a computer system in which memory addresses are 32 bits wide.

ENCM 335 Fall 2018 Slide Set 2 slide 35/56 Address spaces and memory capacity Typically, the capacity of the memory system of a computer is much smaller than the address space. Example: In 2018, a mid-range laptop has 8 GB ( 2 33 bytes) of RAM, and a 64-bit address space. What is the memory capacity expressed as a fraction of the address space size? And a typical program uses much less than the entire memory capacity of a computer. Usually a program has access to only a few relatively small regions within the address space.

ENCM 335 Fall 2018 Slide Set 2 slide 36/56 Memory storage of variables and function parameters If a C variable or function parameter is in memory, it is stored in a group of adjacent bytes a sequence of bytes with consecutive addresses. Let s sketch this out for some common C types, for a few different systems: 1. typical laptop, desktop or server in 2018; 2. 32-bit smartphone or 32-bit embedded system; 3. low-power embedded system designed for minimal battery use.

ENCM 335 Fall 2018 Slide Set 2 slide 37/56 General rules about sizes of types A char is always exactly 1 byte in size. For all other types, sizes vary, depending on hardware (design of processor and memory circuits), operating system, and sometimes on compiler settings. The fact that most sizes are hardware- and OS-dependent is a major headache for C developers trying to port software from one platform to another.

ENCM 335 Fall 2018 Slide Set 2 slide 38/56 The address of a variable The address of a variable the lowest address of all of the addresses of the bytes used for the variable. Let s illustrate that with a sketch of storage for an int variable called x. (To do this, we ll have to make up some addresses for the bytes of x.)

ENCM 335 Fall 2018 Slide Set 2 slide 39/56 Pointers A pointer expression is an expression that has a memory address as a value. A pointer variable is a container for a memory address. A pointer parameter is also a container for a memory address. This is a similar idea to the idea that an int variable or parameter is a container for an integer value within the range of the int type.

ENCM 335 Fall 2018 Slide Set 2 slide 40/56 Example pointer variable declaration int *p; (This use of * has nothing to do with multiplication.) Let s write down a couple of different ways to describe exactly what the example variable declaration means. Attention! The name of the variable here is simply p; it is not *p! The * character is part of the type information, not part of the variable name. I am not fussing unduly over a microscopic detail here trust me, getting this right really helps you to understand the use of pointer variables and parameters!

ENCM 335 Fall 2018 Slide Set 2 slide 41/56 Key operators related to pointer types A binary operator has two operands, but a unary operator has only one. Examples of binary and unary operators: a = b - c; // Here - is binary: subtraction. d = -e; // Here - is unary: negation. The binary * operator does multiplication. The unary * operator is called the pointer dereference operator. Dereferencing a pointer means accessing the data a pointer points to. I ll explain that with examples very soon. The unary & operator is called the address-of operator. (Use of & for address-of is unrelated to the use of & in C++ for reference types.) Let s write out a very simple example use of the address-of operator.

ENCM 335 Fall 2018 Slide Set 2 slide 42/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 43/56 First example program with pointers In textbooks, lecture slides and notes, some program examples resemble production code software that is intended to provide useful services to people or machines. Other examples don t resemble production code at all instead, they re intended to explain programming language features in a way that is as clear and as brief as possible. The upcoming example is definitely in the second of those two categories! Let s copy the program on the next slide, make some remarks about it, and mark points 1 to 5 so we can draw some memory diagrams.

ENCM 335 Fall 2018 Slide Set 2 slide 44/56 #include <stdio.h> int main(void) { int j, k; int *p; p = &j; *p = 3; p = &k; *p = 7; printf("j = %d, k = %d, *p = %d.\n", j, k, *p); return 0; }

ENCM 335 Fall 2018 Slide Set 2 slide 45/56 Arrow notation for diagrams with pointers Writing things like addr. of j in a diagram is inconvenient, so usually we ll use blobs and arrows to indicate which addresses are contained in pointer variables and parameters. Let s redraw the diagram for point 5 in our most recent example, using arrow notation.

ENCM 335 Fall 2018 Slide Set 2 slide 46/56 Addresses are numbers Usually programmers don t know the exact addresses of variables, but to understand the use of pointers, it s sometimes useful to pretend that we do know those addresses. Let s make up some addresses for the bytes of p, k, and j in our most recent example, and draw yet another diagram for point 5.

ENCM 335 Fall 2018 Slide Set 2 slide 47/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 48/56 Function arguments and function parameters #include <stdio.h> x and y are the parameters of the definition of max. The expressions a and b + 10 are arguments within the function call to max. int max(int x, int y) { return (x > y)? x : y; } int main(void) { int a = 41, b = 32, c; c = max(a, b + 10); printf("the answer is %d.\n", c); return 0; }

ENCM 335 Fall 2018 Slide Set 2 slide 49/56 Why is it useful to distinguish parameters from arguments? Having separate terms makes it easy to say useful things like this... The arguments in a function call are all evaluated. Each of the argument values is used to initialize the corresponding parameter value in the definition of the function being called.

ENCM 335 Fall 2018 Slide Set 2 slide 50/56 Terms you might see in programming literature x and y are sometimes called formal arguments of the definition of max. The expressions a and b + 10 within the function call to max are sometimes called actual arguments. #include <stdio.h> int max(int x, int y) { return (x > y)? x : y; } int main(void) { int a = 41, b = 32, c; c = max(a, b + 10); printf("the answer is %d.\n", c); return 0; }

ENCM 335 Fall 2018 Slide Set 2 slide 51/56 Outline of Slide Set 2 An example about types, expressions, and assignment in C Expressions and operators The int and char types are both integer types Introduction to Pointers First example program with pointers Function arguments and function parameters Pointers as function parameters

ENCM 335 Fall 2018 Slide Set 2 slide 52/56 Pointers as function parameters Here s an example problem: Write a C function to convert a measurement in inches only to the equivalent in in feet and inches. For example, 67 inches is 5 feet, 7 inches. Notice that the function receives one number and must communicate back two numbers. The function can t do its job simply by returning an int. The solution is given over the next two slides...

ENCM 335 Fall 2018 Slide Set 2 slide 53/56 #include <stdio.h> void foot_and_inch(int inch_only, int *feet, int *extra_inch); int main(void) { int total_in = 75; int ft; int in; foot_and_inch(total_in, &ft, &in); printf("%d inches is the same as %d ft, %d in.\n", total_in, ft, in); return 0; }

ENCM 335 Fall 2018 Slide Set 2 slide 54/56 void foot_and_inch(int inch_only, int *feet, int *extra_inch) { // point one *feet = inch_only / 12; *extra_inch = inch_only % 12; } // point two What is the program output? Let s write down a bunch of remarks, and make diagrams for point one and point two.

ENCM 335 Fall 2018 Slide Set 2 slide 55/56 A common mistake with pointers What if we changed main to the code on this slide? Let s make some remarks, then explain what is likely to go wrong. // Example of DEFECTIVE code! int main(void) { int total_in = 75; int *ft; int *in; foot_and_inch(total_in, ft, in); printf("%d inches is the same as %d ft, %d in.\n", total_in, *ft, *in); return 0; }

ENCM 335 Fall 2018 Slide Set 2 slide 56/56 A quick remark about the scanf function When scanf was introduced in a lecture, I told you that you must put &, the address-of operator, in front of the names of the variables that are to receive input. At that time, I couldn t give a very precise reason for doing so. But now you know about addresses and pointer expressions. Suppose k is a variable of type int. Why is it that the following code can t possibly work? scanf("%d", k); And what is so important about & in the following code? scanf("%d", &k);