APS105. Pointers. Memory RAM 11/5/2013. Pointers. Need a way to refer to locations of things. a pointer: Address. Example: In C

Similar documents
Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Programming Studio #9 ECE 190

A brief intro to pointers for the purposes of passing reference parameters

Pointers (1A) Young Won Lim 3/5/18

Midterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.

Computer Programming Lecture 12 Pointers

Personal SE. Computer Memory Addresses C Pointers

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

M1-R4: Programing and Problem Solving using C (JAN 2019)

More about BOOLEAN issues

Pointers (1A) Young Won Lim 2/10/18

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Lectures 5-6: Introduction to C

CS 345. Functions. Vitaly Shmatikov. slide 1

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Slides adopted from T. Ferguson Spring 2016

Lecture 2: C Programm

CpSc 1011 Lab 11 Pointers

Lecture 3: C Programm

Pointers (1A) Young Won Lim 2/6/18

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Pointers. Pointers. Pointers (cont) CS 217

Language comparison. C has pointers. Java has references. C++ has pointers and references

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?

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Functions. Arash Rafiey. September 26, 2017

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Lab Instructor : Jean Lai

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Pointers, Dynamic Data, and Reference Types

a data type is Types

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Pointers. Memory. void foo() { }//return

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Module 27 Switch-case statements and Run-time storage management

Chapter 7 C Pointers

Pointers. 10/5/07 Pointers 1

Lectures 5-6: Introduction to C

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

SYSC 2006 C Winter 2012

Tutorial 6. Memory Addresses and Pointers. scanf() function. Perils of Pointers. Function Pointers. Declarations vs. Definitions

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

EECS402 Lecture 02. Functions. Function Prototype

GOLD Language Reference Manual

2.2 Pointers. Department of CSE

Variables, Pointers, and Arrays

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

What is an algorithm?

ANSI C Reserved Words

Study Guide for Test 2

Variables, Memory and Pointers

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Intermediate Programming, Spring 2017*

Lab 3. Pointers Programming Lab (Using C) XU Silei

Programming in C - Part 2

Subject: Fundamental of Computer Programming 2068

Pointers (1A) Young Won Lim 1/14/18

Unit 7. Functions. Need of User Defined Functions

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Lecture 07 Debugging Programs with GDB

Lecture 2:- Functions. Introduction

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Structures and Pointers

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

Exam 3 Chapters 7 & 9

Introduction to Scientific Computing and Problem Solving

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

At the end of this module, the student should be able to:

Arrays and Pointers. Lecture Plan.

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

C/C++ Programming Lecture 18 Name:

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

[0569] p 0318 garbage

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Lecture 04 Introduction to pointers

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

EM108 Software Development for Engineers

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Subprograms. Introduction to Programming (in C++) Subprograms: procedures and functions. Subprograms. Subprograms. Functions are defined as follows:

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Computer Programming: C++

Questions Bank. 14) State any four advantages of using flow-chart

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

Functions:"" Breaking"large"computing"tasks"to" smaller"ones"

Transcription:

APS105 Pointers Textbook Chapter5 1 A Broken Swap Example: a function to swap two values void swap(int x, int y) int temp = x; x = y; y = temp; int main() int a = 1, b = 2; swap(a,b); printf( %d %d\n,a,b); // prints: 1 2 "call by value": only copies values, originals are unchanged Also, x and y are in scope of swap, and don't exist outside of swap How can we modify a,b inside swap? 2 Pointers Need a way to refer to locations of things not just a copy of a variable s value a pointer: points to a variable eg., like a house address points to a house Example: Q: I want to deposit $100 into your bank account A: Great! My balance is $25. Q: uuuuuh, I need to know your account number A bank account number is a pointer to an account need to know the account number to update balance 3 RAM random access memory composed of memory chips organized like a giant table of locations Addresses every location in table has an address just like every house on a street use the address to read/write a location In C addresses are stored in pointer variables a pointer is a memory address Address Value 0 0 1 23 2 0 3 0 2 32-2 102 2 32-1 7 4 1

Declaring a Pointer Declaring a pointer variable int *p; // p is of type integer pointer // p is of type pointer to integer // p is of type (int *) Taking the address of a variable int *p int x; // assign to p the address-of x // p points-to x Using Pointers Declaring and assigning int *p; // p is loc w addr 2 int x; // x is loc w addr 3 x = 8; // x = 8 // p = addr-of x Addr Value 0 0 1 23 2 0 3 0 2 32-2 102 2 32-1 7 3 p 8 x NOTE: we usually don't care about address values 5 6 int *p; int x = 8; Dereferencing a Pointer // p points to x int y = *p; // assign to y the value // pointed to by p // * is called a dereference operator // *p means "contents of the location pointed to by p // *p means "what's at p" Example Using Pointers Declaring, assigning, and using a pointer int *p; int x = 8; p x 8 printf("%d\n",*p); // print what's at p // prints 8 7 8 2

double x, y; double *p, *q; x = 3.6; y = 6.7; q = &y; *p = 1.0; *q = *p + 1.0; Pointers Example Addr 0 0 1 0 2 0 3 0 4 0 5 0 2 32-2 0 2 32-1 0 3.6 6.7 2 3 Value 1.0 2.0 x y p q Pointers Example Again double x, y; double *p, *q; x = 3.6; y = 6.7; q = &y; *p = 1.0; *q = *p + 1.0; p q x 3.6 1.0 y 6.7 2.0 9 10 Fixed Swap Example: a function to swap two values void swap(int *p, int *q) int temp = *p; *p = *q; *q = temp; int main() int a = 1, b = 2; swap(&a,&b); printf( %d %d\n,a,b); // prints: 2 1 still "call by value": only copies values, but we're copying address values and the addresses point to the real locations of a and b Functions that Return Pointers Create a function called largeloc Takes addresses of two doubles Returns the address of the larger value Example usage: double x = 2.6; double y = 3.4; double *p; p = largeloc(&x,&y); // p gets set to address of y 11 12 3

largeloc double *largeloc(double *a,double *b) if (*a >= *b) return a; return b; Scope 13 14 Scope of Internal Identifiers Scope of an identifier: the range within which it is recognized an identifier is not recognized outside its scope i.e., it cannot be used, or compiler will complain Ex: for (int i = 1;...;...) scope of i is within the for loop only Scope of a function parameter: only within the body of the function Scope of a variable declared in a function starts at the point of declaration ends at the end of the block it was declared in called an internal identifier 15 Scope Example int foo(int x) int y=5; for (int i=0;i<10;i++) int z = 3; y *= z + x; return y; 16 4

Scope Example Overlapping Scope int foo(int x) int y=5; for (int i=0;i<10;i++) int z = 3; y *= z + x; scope of z scope of i scope of y scope of x int i = 1; printf( i = %d\n,i); int i = 2; printf( i = %d\n,i); printf( i = %d\n,i); return y; What is the output? i = 1 i = 2 i = 1 not recommended!! easy to mess up 17 18 Scope of External Identifiers External identifier one that is declared outside of any function ex: a function identifier/name is external Example: int x; // x is an external identifier void main()... Modular Program Design If external ident. declared before all func.s: then it is called global 19 20 5

Modular Program Design Functions and scope provide modularity can build them independently and combine Modularity eases large programs break the problem into smaller & smaller parts until the parts are small and manageable make these into functions combine them into the whole 21 Example: Mersenne Numbers Prime Number: A num divisble only by 1 and itself is prime 1is not prime Mersenne Number: A prime number M such that M = 2n 1 Where n is also prime Note: There are 48 known Latest discovered 2013 January 25 The largest known prime is Mersenne 2 57,885,161 1 22 Print the first N mersenne primes bool isprime(int x) if (x <= 1) return false; for (int i=2;i<=x/2;i++) if (x % i == 0) return false; return true; bool ismersenne(int x) if (x <= 3) return false; return isprime(x) && isprime((x+1)/2); void printmersenne(int N) for (int i=1;i<n;i++) if (ismersenne(i)) printf("mersenne: %d = %d * 2-1\n",i,(i+1)/2); 23 6