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

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

type arrayname [ size type arrayname [ size ] = { item ,, item size size

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

Storage Class Specifiers Scope and Lifetime of Variables CMPE-013/L. Modules and Scope. Storage Class Specifiers Automatic Variables

CMPE-013/L. Introduction to C Programming

Functions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014.

CMPE-013/L. Introduction to C Programming. Unit testing

Pointers, Arrays and Parameters

Memory, Arrays & Pointers

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

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Exercise 1.1 Hello world

Fall 2018 Discussion 2: September 3, 2018

Arrays and Pointers in C. Alan L. Cox

Pointers, Dynamic Data, and Reference Types

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

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

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

[0569] p 0318 garbage

What is an algorithm?

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

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

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

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Pointers CMPE-013/L. Pointers. Pointers What do they do? Pointers What are pointers? Gabriel Hugh Elkaim Winter 2014

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

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

FUNCTIONS POINTERS. Pointers. Functions

Lectures 5-6: Introduction to C

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Basic and Practice in Programming Lab7

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

Lecture 9 - C Functions

BİL200 TUTORIAL-EXERCISES Objective:

C++ Programming Chapter 7 Pointers

by Pearson Education, Inc. All Rights Reserved.

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

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

Strings Investigating Memory Allocation Pointers Fixed-Point Arithmetic. Memory Matters. Embedded Systems Interfacing.

CMSC 202 Midterm Exam 1 Fall 2015

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

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

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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

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

C Pointers. 6th April 2017 Giulio Picierro

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

EL6483: Brief Overview of C Programming Language

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Chapter IV Introduction to C for Java programmers

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

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?

CS 2461: Computer Architecture I

Introduction to Scientific Computing and Problem Solving

Dynamic arrays / C Strings

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Intermediate Programming, Spring 2017*

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

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Computer Programming: Skills & Concepts (CP) Strings

What have we learned about when we learned about function parameters? 1-1

Functions. Arash Rafiey. September 26, 2017

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Procedural Programming & Fundamentals of Programming

For example, let s say we define an array of char of size six:

1 Pointer Concepts. 1.1 Pointer Examples

today cs3157-fall2002-sklar-lect05 1

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

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

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

SYSC 2006 C Winter 2012

Lecture 05 POINTERS 1

Intermediate Programming, Spring 2017*

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

Lectures 5-6: Introduction to C

CMPE-013/L. Expressions and Control

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Object oriented programming C++

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

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

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

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

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

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

CS 222: Pointers and Manual Memory Management

Lecture 04 Introduction to pointers

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CS 0449 Sample Midterm

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

Arrays. Week 4. Assylbek Jumagaliyev

Transcription:

CMPE-013/L Gabriel Hugh Elkaim Winter 2014 Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by value) int x = 2, y = 0; Value of variable passed to function is copied into local variable n int square(int n) return (n * n); int main(void) y = square(x); After Function Call: y = 4 x= 2 xwas notchanged by function Passing Pointers to Functions Pointers allow a function to operate on the original variable (pass by reference) int x = 2, y = 0; void square(int *n) *n *= *n; int main(void) square(&x); Address of variable passed to function and stored in local pointer variable n After Function Call: x= 4 x was changed by function A function with a pointer parameter: Example Passing Pointers to Functions int foo(int *q) Must be called in one of two ways: (assume: int x, *p = &x;) foo(&x) foo(p) Pass an address to the function so the address may be assigned to the pointer parameter: q = &x Pass a pointer to the function so the address may be assigned to the pointer parameter: q = p 1

Example Part 1 Passing Parameters By Reference Swap function definition: void swap(int *n1, int *n2) int temp; temp = *n1; *n1 = *n2; *n2 = temp; We know where you live! Addresses of parameters copied to local pointer variables: Function can now modify the original variables via pointers. Example Part 2 Passing Parameters By Reference Main function definition: int main(void) int x = 5, y = 10; int *p = &y; swap(&x, p); while(1); Swap function prototype: void swap(int *n1, int *n2) After running program: x= 10 y= 5 Tell function where x and ylive n1 = &x n2 = p So far, we have worked with strings strictly as arrays of char Strings may be created and used with pointers much more elegantly String declaration with a pointer: char *str = "PIC"; Implementation varies depending on compiler and architecture used. I \0 P C str 16-bit Data Memory (RAM) 91C0 4950 0043 Address 0x08C2 0x91C0 0x91C2 When initialized, a pointer to a string points to the first character: char *str = "Microchip"; str M i c r o c h i p \0 str += 4 Increment or add an offset to the pointer to access subsequent characters 2

Pointers may also be used to access characters via an offset: char *str = "Microchip"; *str == 'M' M i c r o c h i p \0 *(str + 4) == 'o' Pointer always points to "base address" Offsets used to access subsequent chars Example: Pointer Variable Pointer versus Array: Initialization at Declaration Initializing a character string when it is declared is essentially the same for both a pointer and an array: char *str = "PIC"; Example: Array Variable char str[] = "PIC"; char str[4] = "PIC"; The NULL character '\0' is automatically appended to strings in both cases (array must be large enough). or Example: Pointer Variable Pointer versus Array: Assignment in Code An entire string may be assigned to a pointer A character array must be assigned character by character char *str; str = "PIC"; Example: Array Variable char str[4]; str[0] = 'P'; str[1] = 'I'; str[2] = 'C'; str[3] = '\0'; Must explicitly add NULL character '\0' to array. Comparing Strings If you want to test a string for equivalence, the natural thing to do is: if (str == "Microchip") This is notcorrect, though it might appear to work sometimes This compares the address in strto the address of the string literal "Microchip" The correct way is to use the strcmp()function in the standard library which compares strings character by character 3

Comparing Strings strcmp() prototype: Function Prototype int strcmp(const char *s1, const char *s2); strcmp() return values: <0 if s1 is less than s2 0 if s1 is equal to s2 >0 if s1 is greater than s2 Example Comparing Strings #include <string.h> char *str = "Microchip"; int main(void) if (0 == strcmp(str, "Microchip")) printf("they match!\n"); The strcmp() prototype is in C:\Program Files\Microchip\MPLAB C30\include\string.h while(1); Arrays of Pointers Declaration An array of pointers is an ordinary array variable whose elements happen to all be pointers. char *p[4]; This creates an array of 4 pointers to char The array p[]itself is like any other array The elements of p[], such as p[1], are pointers to char p[0] p[1] p[2] p[3] Arrays of Pointers Array Elements are Pointers Themselves 16-bit Data Memory (RAM) 0000 91C0 91C3 91C7 91CC 0000 0000 0000 91C0 O n \0 91C3 O f f \0 91C7 M a i n \0 91CC A u x \0 4

Arrays of Pointers Initialization A pointer array element may be initialized just like its ordinary variable counterpart: p[0] = &x; Or, when working with strings: p[0] = "My string"; Arrays of Pointers Dereferencing To use the value pointed to by a pointer array element, just dereference it like you would an ordinary variable: y = *p[0]; Using *p[0]is the same as using the object it points to, such as xor the string literal "My String" from the previous slide Example Arrays of Pointers Accessing Strings int i = 0; char *str[] = "Zero", "One", "Two", "Three", "Four", "\0"; int main(void) while(*str[i]!= '\0') printf("%s\n", str[i++]); Lab Exercise 12 Pointers, Arrays, and Functions while(1); 5

Open the lab Project: Lab 12 Pointers, Arrays, and Functions On the class website /Examples/Lab12.zip -> Load Lab12.X 1 Open MPLAB X and select Open Project Icon (Ctrl + Shift + O) Open the Project listed above. If you already have a project open in MPLAB X, close it by right clicking on the open project and selecting Close Exercise 12 Pointers, Arrays, and Functions Solution: Steps 1 and 2 /*############################################################################ # STEP1: Pass the variable x to the function twoscomplement such that the # value of x itself may be changed by the function. Note: The function # expects a pointer (address) as its parameter. ############################################################################*/ //Perform twos complement on x twoscomplement(&x); /*############################################################################ # STEP 2: Pass the array 'a' to the function reverse1(). Use the constant # ARRAY_SIZE for the second parameter. # See definition of function reverse1() below. ############################################################################*/ //Reverse order of elements by passing array reverse1(a, ARRAY_SIZE); Exercise 12 Pointers, Arrays, and Functions Solution: Steps 3 and 4 /*############################################################################ # STEP 3: Pass a pointer to array 'a' to the function reverse2(). Use the # constant ARRAY_SIZE for the second parameter. # See definition of function reverse2() below. # Hint: You do not need to define a new pointer variable to do this. ############################################################################*/ //Reverse order of elements by passing pointer reverse2(a, ARRAY_SIZE); /*############################################################################ # STEP 4: Complete the function header by defining a parameter called 'number' # that points to an integer (i.e. accepts the address of an integer # variable). ############################################################################*/ //void twoscomplement(/*### Your Code Here ###*/) void twoscomplement(int *number) *number = ~(*number); //Bitwise complement value *number += 1; //Add 1 to result Exercise 12 Conclusions Pointers make it possible to pass a variable by reference to a function (allows function to modify original variable not a copy of its contents) Arrays are frequently treated like pointers An array name alone represents the address of the first element of the array 6

Questions? 7

8

9

10

11