142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34……
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Copyright©1998 Angus Wu PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING EE PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Introduction to C Programming CE
Topic 8 – Introduction to Pointers and Function Output Parameters.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Computer Science 210 Computer Organization Pointers.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Copyright © – Curt Hill Pointers A Light Introduction.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions & Pointers in C Jordan Erenrich
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
Pointers *, &, array similarities, functions, sizeof.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
CSE 220 – C Programming Pointers.
EPSII 59:006 Spring 2004.
Chapter 2 Overview of C.
Pointers.
INC 161 , CPE 100 Computer Programming
Basic notes on pointers in C
Local Variables variables which are declared within a
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Programming in C Pointer Basics.
Pass by Reference.
Initializing variables
C++ Pointers and Strings
Pointers and dynamic objects
C++ Pointers and Strings
FUNCTION ||.
Presentation transcript:

142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4; b=7; add_one(a,b); printf("a=%i b=%i",a,b); return 0; } What is printed? a=4 b=7

What is going on ? 142 L -2 ab main xy add_one XX (doesn't return anything!)

142 L -3 Values versus locations 4 a 1012 Consider a memory box: If we want add_one to really add 1 to a and b (and not just to a copy of their value), add_one needs access to the locations of a and b as well as to their value. to know where the memory boxes are. to know what the boxes contain Variable name for a location which holds a value

142 L -4 Can we have access to the location number (= address) of a variable? Yes! Use a pointer New type: pointer variable a pointer contains the reference to another variable, i.e. the value of a pointer is the address of a variable Example: int x; regular integer variable x int *xp; xp is a variable of type pointer to an integer xp to assign the address of x to xp: xp=&x; 10115

142 L - 5 Using a pointer relevant unary operator: & and * read & as: address of read * as: content of the location pointed to by xp = &x; assign the address of x to xp *xp = 0; *xp = *xp + 1; assign integer 0 to x add 1 to x

142 L -6 add_one with pointers void add_one(int *xp, int *yp) { *xp = *xp + 1; *yp = *yp + 1; } int main(void) { int a,b; a=4; b=7; add_one(&a,&b); printf("a=%i b=%i",a,b); return 0; } What is printed? a=5 b=8

4 What is going on ? 142 L -7 7 a b main xp yp add_one points to a points to b

142 L -8 Addresses and Pointers Three new types: int * pointer to an int double * pointer to a double char * pointer to a char Two new unary operators: & address of (can be applied to any variable) * content of the location pointed to by (can be applied ONLY to a pointer) Note: to print the value of a pointer, use the placeholder %p printf("&i=%p",&i);

142 L -9 Vocabulary Dereferencing: Following a pointer to a memory location Output parameters: int *iptr; int i,j; i=0; iptr = &i; j = *iptr + 1; e.g. dereferencing use iptr to point to i same as j=i+1; up to now, parameters of a function have always been input parameters. With a pointer as a parameter of a function: _ as usual can provide a value to the function (input) _ can get a value back from the function via a parameter (output). Before, could get only one value back from a function. With pointers, as many as we want. void add_one(int *xp, int *yp){...} add_one(&a,&b); input and output

Why use pointers? 142 L -10 as output parameters Functions that need to change the value of their actual parameters (e.g. add_one ) to get multiple return values Functions that need to pass back several results Advanced programming Operations on arrays, linked lists (see chapter 14)

An Example 142 L -11 Swap 2 integers: Given c1 and c2, exchange their values if c2<c1 Without pointers: int c1, c2, temp; printf("Enter two integers: "); scanf("%i %i",&c1,&c2); if (c2<c1) { temp = c1; c1 = c2; c2 = temp; } With pointers: we can use a function if (c2<c1) swap(&c1,&c2); replace that with this and for the swap function void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; }

Another Example (1) 142 L -12 Order 3 integers: Given x,y and z, order x, y and z in increasing order One way (using swap from the previous example): int x,y,z; printf("Enter three integers: "); scanf("%i %i %i",&x, &y, &z); if (x>y) swap(&x,&y); if (x>z) swap(&x,&z); if (y>z) swap(&y,&z); order counts!

Another way: Having just one function call in main int main(void) { int x,y,z; printf("Enter three integers: "); scanf("%i %i %i",&x, &y, &z); sort3(&x,&y,&z);... } void sort3(int *xp, int *yp, int *zp) { if (*xp>*yp) swap(xp,yp); if (*xp>*zp) swap(xp,zp); if (*yp>*zp) swap(yp,zp); } no & 142 L -13 Another Example (2)

Beware of types ! 142 L -14 int i; int *ip; double x; double *xp; x = i;OK i = x;Not recommended ip = 30; No! ip = i; No! ip = &i;OK ip = &x;No! xp = ip;No! &i = ip;Meaningless (compilation error)

Understand & in scanf 142 L - 15 A function to read the letter a or b: void scan_a_or_b(char *chp) { printf("Enter an 'a' or a 'b': "); scanf("%c",chp); while(*chp != 'a' && *chp != 'b') { printf("What?\n"); printf ("Enter an 'a' or a 'b': "); scanf("%c",chp); } and in main: char answer; scan_a_or_b(&answer); No & don't say Always use & in scanf say Always use an address in scanf