CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Lecture 5 Pointers 1. Variable, memory location, address, value
Pointers and Dynamic Arrays
User-Written Functions
UNIT 5 C Pointers.
Standard Version of Starting Out with C++, 4th Edition
© 2016 Pearson Education, Ltd. All rights reserved.
POINTERS.
POINTERS.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
INC 161 , CPE 100 Computer Programming
Pointer.
Pointers.
Chapter 10: Pointers 1.
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
5.1 Introduction Pointers Powerful, but difficult to master
Chapter 7: User-Defined Functions II
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C++ Programming Lecture 17 Pointers – Part I
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers and pointer applications
Presentation transcript:

CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers

Objectives In this chapter you will learn about,  Basic concept of pointers  Pointer variables  Pointer declaration  Pointer definition and initialization  Pointer operator (& and *)  Parameter passing by reference prepared by NI, edited by MAF

What is a pointer  So far, we have seen that a variable is used to store a value.  Variables allow the programmer to directly manipulate the data in memory.  A pointer variable, however, does not store a value but store the address of the memory space which contain the value i.e. it directly points to a specific memory address.  Why would we want to use pointers?  To call a function by reference so that the data passed to the function can be changed inside the function.  To create a dynamic data structure which can grow larger or smaller as necessary. prepared by NI, edited by MAF

Variable declaration  A variable declaration such as, int number = 20; causes the compiler to allocate a memory location for the variable number and store in it the integer value 20. This absolute address of the memory location is readily available to our program during the run time. The computer uses this address to access its content. prepared by NI, edited by MAF number 20 number directly references a variable whose value is

Pointer declaration  General Format: data_type *pointer_name;  A pointer declaration such as, int *numberPtr; declares numberptr as a variable that points to an integer variable. Its content is a memory address.  The * indicates that the variable being declared is a pointer variable instead of a normal variable. prepared by NI, edited by MAF

Pointer declaration cont…  Consider the following declaration int *numberPtr, number = 20;  In this case, two memory address have been reserved in the memory, namely the numberPtr and number.  The value in variable number is of type integer, and the value in variable numberPtr is an address for another memory. prepared by NI, edited by MAF number*numberPtr

Pointer Initialization  To prevent the pointer from pointing to a random memory address, it is advisable that the pointer is initialized to 0, NULL or an address before being used.  A pointer with the value NULL, points to nothing.  Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred. prepared by NI, edited by MAF

Pointer Operator (& and *)  When a pointer is created, it is not pointing to any valid memory address. Therefore, we need to assign it to a variable’s address by using the & operator. This operator is called a reference operator.  Look at this example: int number = 20; int *numberPtr; numberPtr = &number; printf(“number = %d”, *numberPtr);  The statement numberPtr = &number assigns the address of the variable number to a pointer variable numberPtr. Variable numberPtr is then said as to “point to” variable number. prepared by NI, edited by MAF Output: number = 20

Graphical representation  int *numberPtr, number = 20;  numberPtr = &number; prepared by NI, edited by MAF number*numberPtr number*numberPtr

Pointer Operator (& and *) cont…  After a pointer is assigned to a particular address, the value in the pointed address can be accessed/modified using the * operator.  This operator is commonly called as the indirection operator or dereferencing operator.  The * operator returns the value of the object to which its operand points. For example, the statement  printf(“number = %d”, *numberPtr); prints the value of variable number, namely as 20. Using * in this manner is called dereferencing operator. prepared by NI, edited by MAF

Example: & and * #include void main(void) { int var = 10; int *ptrvar = &var; printf(“The address of the variable var is: %d\n”, &var); printf(“The value of the pointer ptrvar is: %d\n”, ptrvar); printf(“Both values are the same\n”); printf(“The value of the variable var is: %d\n”, var); printf(“The value of *ptrvar is: %d\n”, *ptrvar); printf(“Both values are the same\n”); printf(“The address of the value pointed by ptrvar is: %d\n”, &*ptrvar); printf(“The value inside the address of ptrvar is: %d\n”, *&ptrvar); printf(“Both values are the same\n”); } prepared by NI, edited by MAF

Example: & and * /*Sample Output */ The address of the variable var is: The value of the pointer ptrvar is: Both values are the same The value of the variable var is: 10 The value of *ptrvar is: 10 Both values are the same The address of the value pointed by ptrvar is: The value inside the address of ptrvar is: Both values are the same Press any key to continue prepared by NI, edited by MAF

&* and *&  & and * are inverse operations. &* acts equivalent to *& and this leads back to the original value.  Example: (Assume that the address of num is ) #include void main(void) { int num = 5; int *numPtr = # printf("%d \n", numPtr); printf("%d \n", &*numPtr); printf("%d \n", *&numPtr); } prepared by NI, edited by MAF Output:

EXERCISE IN CLASS PART 1

QUESTION 1 void main() { int u=3, v; int *pu, *pv; pu=&u; v=*pu; pv=&v; printf("\nu=%d &u=%d pu=%d *pu=%d", u, &u, pu, *pu); printf("\n\nv=%d &v=%d pv=%d *pv=%d", v, &v, pv, *pv); }

QUESTION 2 void main() {int u1, u2, v=3; int *pv; u1= 2 * (v + 5); pv=&v; u2= 2* (*pv + 5); printf("\nu1=%d u2=%d", u1,u2); }

QUESTION 3 void main() {int p, q, *temp1, *temp2; p = 10, q = 20; temp1 = &q; temp2 = &p; *temp1 = *temp2 + 2; p = *temp1; q = *temp1 + *temp2; } Trace out the value of q, p, *temp1 and *temp2.

QUESTION 4 int m, n, *p, *q; m = 5; n = 7; p = &m; q = p; *q = m + n; q = &n; n = m + *q; Trace out the value of m, n, *p and *q.

Take out a clean A4 paper..... Write your NAME, ID, SECTION and DATE.... It's time for a pop quiz!

QUIZ – (a) int i = 2, k = 4, *p1, *p2; p1 = &i; p2 = &k; *p1 = *p2; *p1 = 6; *p2 = 8; Write the value of i, k, *p1 and *p2 (i) int i = 42, k = 80, *p1,*p2; p1 = &i; p2 = &k; *p1 = *p2; (ii)

Parameter Passing by Reference/Pointer  A function may return multiple values by declaring their formal parameters (passing value) as pointers variables.  This way of passing the argument is known as call by reference  When the value referenced by the pointer is changed inside the function, the value in the actual variable will also change.  Therefore, we can pass the result of the function through the function argument without having to use the return statement. prepared by NI, edited by MAF

Parameter Passing by Reference/Pointer  When a pointer is passed to a function, we are actually passing the address of a variable to the function.  Since we have the address, we can directly manipulate the data in the address.  In the case where a non-pointer variable is passed, the function will create another space in memory to hold the value locally while the program is inside the function. Therefore, any change to the variable inside the function will not change the actual value of the variable. prepared by NI, edited by MAF

Parameter Passing by Reference/Pointer  To pass the value of variable by pointers between two functions, we must do the following:  Declare the variable that is meant to return a value to the calling function as a pointer variable in the formal parameter list of the function. void function_name(int *varPtr);  When to call the function, use a variable together with address operator (&) function_name(&var); prepared by NI, edited by MAF

Parameter Passing by Reference/Pointer  Note : the effect of the previous two actions is the same as the effect of the declarations int var; int *varPtr=&var;  Declare the function with pointer parameter appropriately in a function prototype by using symbol * as a prefix for pointer parameters. void function_name(int *); prepared by NI, edited by MAF

Example #include void Func1(int, int); void Func2(int *, int *); void main( ) { int a = 8, b = 9; printf(“Before Func1 is called, a = %d, b = %d\n”, a, b); Func1(a, b); printf(“After Func1 is called, a = %d, b = %d\n”, a, b); printf(“\nBefore Func2 is called, a = %d, b = %d\n”, a, b); Func2(&a, &b); printf(“After Func2 is called, a = %d, b = %d\n”, a, b); } prepared by NI, edited by MAF

Example void Func1(int a, int b) { a = 0; b = 0; printf(“The value inside Func1, a = %d, b = %d\n”, a, b); } void Func2(int *pa, int *pb) { *pa = 0; *pb = 0; printf("The value inside Func2, *pa = %d, *pb = %d\n\n", *pa, *pb); } prepared by NI, edited by MAF

Result /* output */ Before Func1 is called, a = 8, b = 9 The value inside Func1, a = 0, b = 0 After Func1 is called, a = 8, b = 9 Before Func2 is called, a = 8, b = 9 The value inside Func2, *pa = 0, *pb = 0 After Func2 is called, a = 0, b = 0 Press any key to continue prepared by NI, edited by MAF

EXERCISE IN CLASS PART 2

Refer to “Pointer_Exercise.doc” QUESTION 1

It's time for a pop quiz! Again?????

Refer to “Pointer_Quiz.doc” Quiz – (b)

Summary  In this chapter, you have learnt  The pointer operator: & and *  Pointer declaration and assignment  Parameter passing by reference  The 3 steps needed to pass the value of variable by pointers between two functions.  Declare the variable as a pointer variable in formal parameter list of the function.  Use a variable together with address operator (&) when to call a function.  Using symbol * in function prototype to declare any pointer parameter. prepared by NI, edited by MAF

THAT’S ALL FOR TODAY…… SEE YA NEXT CLASS