Pointers and scanf() Steven R. Bagley

Similar documents
Procedures, Parameters, Values and Variables. Steven R. Bagley

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

FUNCTIONS POINTERS. Pointers. Functions

Programming and Data Structure

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CSE 333 Midterm Exam 7/29/13

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Programming in C. Pointers and Arrays

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Overview (1A) Young Won Lim 9/14/17

2/6/2018. ECE 220: Computer Systems & Programming. Function Signature Needed to Call Function. Signature Include Name and Types for Inputs and Outputs

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

C introduction: part 1

Overview (1A) Young Won Lim 9/9/17

CS113: Lecture 4. Topics: Functions. Function Activation Records

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

COMP 208 Computers in Engineering

Overview (1A) Young Won Lim 9/25/17

G52CPP C++ Programming Lecture 9

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Fundamentals of Programming & Procedural Programming

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

Lecture 04 Introduction to pointers

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Dynamic memory allocation

Final CSE 131B Spring 2004

COMP1917: 09 Arrays and Strings

C Functions. 5.2 Program Modules in C

Flow Control. CSC215 Lecture

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Strings. Steven R. Bagley

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

ALGORITHM 2-1 Solution for Exercise 4

Fundamental of Programming (C)

Heap Arrays and Linked Lists. Steven R. Bagley

C Language Coding Standard

EE 312 Fall 2017 Midterm 1 October 12, 2017

Program Organization and Comments

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Functions. Systems Programming Concepts

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

Heap Arrays. Steven R. Bagley

CSE 333 Midterm Exam Sample Solution 7/29/13

C Exercises. Ben Vandiver February 28, 2003

Fast Introduction to Object Oriented Programming and C++

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Arrays and Pointers in C. Alan L. Cox

Array Initialization

Tutorial 10 Pointers in C. Shuyue Hu

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

211: Computer Architecture Summer 2016

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

1. The keyword main in C language is used for

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

WARM UP LESSONS BARE BASICS

2. Numbers In, Numbers Out

EL2310 Scientific Programming

Conditional Expressions

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

CS 139 Practice Midterm Questions #2

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Control Structure: Loop

POINTER AND ARRAY SUNU WIBIRAMA

C programming basics T3-1 -

CSE 374 Programming Concepts & Tools

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

York University Faculty Science and Engineering Fall 2008

CISC 1600 Lecture 3.1 Introduction to Processing

Text Input and Conditionals

Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

Solution Set(Reference Book - Programming with c By Byron Gottfried Thrid Edition)

Exercises C-Programming

Structured Programming. Functions and Structured Programming. Functions. Variables

The Dynamic Typing Interlude

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

Function a block of statements grouped together, to perform some specific task

CS16 Exam #1 7/17/ Minutes 100 Points total

COMP 2355 Introduction to Systems Programming

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

UNIT III (PART-II) & UNIT IV(PART-I)

Chapter 11 Introduction to Programming in C

Functions. (transfer of parameters, returned values, recursion, function pointers).

ELEC 377 C Programming Tutorial. ELEC Operating Systems

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

Computer Science & Engineering 150A Problem Solving Using Computers

Transcription:

Pointers and scanf() Steven R. Bagley

Recap Programs are a series of statements Defined in functions Can call functions to alter program flow if statement can determine whether code gets run Loops can execute code multiple times

Keyboard Input Exercise 3 requires you to use scanf() to read two ints from the keyboard It has a new syntax scanf( %d,%d, &width, &height); This is because scanf can read more than one value But functions can only return one

Function calls Function calls can take parameters The parameter passes the value not the variable This means that if the function alters the value of the parameter the original variable won t be affected

void func(int x) { x++; printf( Printing from func, x = %d\n, x); } int main(int argc, char *argv[]) { int a = 42; } printf( In main, a = %d\n, x); func(a); printf( In main again, a = %d\n, a); Run this program -- should print out 42, 43, 42

int a = 42; func(a);

int a = 42; func(a); a

int a = 42; a 42 func(a);

int a = 42; a 42 func(a); void func(int x) { x++; printf( Printing from func, x = %d\n, x); }

int a = 42; a 42 func(a); void func(int x) x { x++; printf( Printing from func, x = %d\n, x); }

int a = 42; a 42 func(a); void func(int x) { x++; x 42 printf( Printing from func, x = %d\n, x); }

int a = 42; a 42 func(a); void func(int x) { x++; x 43 printf( Printing from func, x = %d\n, x); }

int a = 42; a 42 func(a); void func(int x) { x++; printf( Printing from func, x = %d\n, x); }

int a = 42; a 42 func(a);

Parameters When a function is called, a copy of the value is passed Copy placed in the parameter variable Functions can alter the value of this parameter But because it only contained a copy it doesn t affect the original

Return Values Functions can return a single value Use return keyword The return value is copied and passed back to the original function Where it does something with it (assign to variable, pass as another parameter, etc )

Return Values What if you need to return more than two values? For example, solving a quadratic equation Each quadratic equation has two possible answers How would we write a function to calculate these?

Solving the equation means finding the two values of x which mean y is 0

3 2 1 5-4 -3-2 -1 0 1 2 3 4 5-1 -2-3 y = 2x 2 + 3x - 1 Solving the equation means finding the two values of x which mean y is 0

3 2 1 5-4 -3-2 -1 0 1 2 3 4 5-1 -2-3 y = 2x 2 + 3x - 1 Solving the equation means finding the two values of x which mean y is 0

double solvequadratic(double a, double b, double c) { double x; } x = (-b + sqrt(b*b - 4*a*c))/(2*a); return x; sqrt() calculates the square root, and requires to include math.h This calculates the positive root, but what about the negative root

Multiple returns Can sometimes get around it by writing two functions But we end up calculating things twice In other cases, you can t write it as multiple functions e.g. scanf() Fortunately, C provides with a solution

double solvequadraticneg(double a, double b, double c) { double x; } x = (-b - sqrt(b*b - 4*a*c))/(2*a); return x; sqrt() calculates the square root, and requires to include math.h This calculates the negative root

Value, or where to find it? So far, we ve been passing the value to a parameter But we could also pass something else The address of where to find the value All variables are stored in the computer s memory at a specific address

Point at the value If we know the address of the variable, then we can modify its contents Think about what you ve done in CSA So if we pass the function the address of the variable, rather than its value it could alter the original variable Envelope demonstration

Pointers Tend to call variables that contain the address of another variable, a pointer The variable points at some value (somewhere over there) Defined by using a * when declaring the variable int *p; p is a variable that points to an int (somewhere in memory) http://www.cdecl.org

Using Pointers Can assign a value to p just as before But this sets where p points To access what p points at, you need to place a * before it, e.g. printf( %d, *p); *p = 42; Print the integer that is pointed at by p Accessing the value pointed to is sometimes called dereferencing

Address of Need to assign the pointer to point to something (like another variable) Do this by using the address-of operator, & So &x gets the address of the variable, x Note the types should match, an int pointer should point to an int and so on

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x y x = 42; p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x y x = 42; p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 42 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 42 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 42 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 42 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 31 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 31 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 31 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 21 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 31 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

int x,y; /* Declare two integers, x and y */ int *p; /* Declare a pointer to an integer */ x = 42; x y 31 42 p = &x; /* Set p to point at the address of x */ printf( %d, *p); /* print value pointed to by p, 42 */ x = 31; printf( %d, *p); /* print value pointed to by p, 31 */ *p = 21; /* Set the int that p points at to 21 */ printf( %d, x); /* prints 21 */ p = &y; /* p now points at y */ *p = 42; /* value of y changed, but x left the same*/ p

Returning Multiple Values Can use this to return multiple values in our quadratic function Takes five parameters now The three co-efficients, a, b, and c Two pointers, *pos, and *neg which point to where to store the result What do we return? Return nothing in this case, could also return a status value, or one of the answers

double solvequadratic(double a, double b, double c, double *pos, *neg) { double x; double s, twoa; twoa = 2 * a; s = sqrt(b*b - 4 * a * c); } *pos = (-b + s) / twoa; *neg = (-b - s) / twoa; sqrt() calculates the square root, and requires to include math.h Calculates both roots

Pointers Pointers are incredibly powerful We shall see them used all over the place Both visibly like here, and behind the scenes (e.g. in Java) This is only scratching the surface We shall return to them