DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CS 192 Lecture 12 Winter 2003 December 31, January 1, 2004 Dr. Shafay Shamail.
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.
Tuesday, January 16, 2007 How would a car function if it were designed like a computer? Occasionally, executing a maneuver would cause your car to stop.
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.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Basic Elements of C++ Chapter 2.
Chapter 8 Pointers C++ How to Program, 9/e
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
A Sample Program #include using namespace std; int main(void) { cout
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Basic Elements of C++.
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Pointer-Based Strings
CNG 140 C Programming (Lecture set 10)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Basic Elements of C++ Chapter 2.
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
CISC181 Introduction to Computer Science Dr
Presentation transcript:

DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Pointer After completing this chapter student should be able to:-

What Are Pointers? A pointer is a variable that contains a memory address. For example, if x contains the address of y, then x is said to "point to" y. Pointer declaration –type *var-name; –For example, to declare p to be a pointer to an integer, use this declaration: –int *p; –For a float pointer, use –float *p;

The Pointer Operators There are two special operators that are used with pointers: * and &. The & is a unary operator that returns the memory address of its operand. –balptr = &balance; puts into balptr the memory address of the variable balance. The * is a unary operator that returns the value of the variable located at the address –value = *balptr; will place the value of balance into value.

Consider the following program:- 1.#include 2.using namespace std; 3.int main() 4.{ 5.int balance; 6.int *balptr; 7.int value; 8.balance = 3200; 9.balptr = &balance; 10.value = *balptr; 11.cout << "balance is: " << value << '\n'; 12.return 0; 13.}

Figure 6-1 depicts the actions of the two preceding statements.

The Pointer Operators(2) What is the output of the program above? Balance is : 3200

The Base Type Is Important Your pointer variables must always point to the correct type of data. When you declare a pointer to be of type int, the compiler assumes that anything it points to will be an integer value. For example, the following fragment is incorrect: int *p; double f; //... p = &f; // ERROR

The Base Type Is Important(2) You can override this restriction (at your own risk) by using a cast. For example, the following fragment is now technically correct: int *p ; double f; //... p = (int *) &f; // Now technically OK To better understand why using a cast to assign one type of pointer to another is not usually a good idea, consider the following short program:

1.// This program will not work right. 2.#include 3.using namespace std; 4.int main() 5.{ 6.double x, y; 7.int *p; 8.x = ; 9.p = (int *) &x; // use cast to assign double * to int * 10.y = *p; // What will this do? 11.cout << y; // What will this print? 12.return 0; 13.}

Assigning Values Through a Pointer You can use a pointer to assign a value to the location pointed to by the pointer. Example. Assuming that p is an integer pointer, this assigns the value 101 to the location pointed to by p: *p = 101; ("at the location pointed to by p, assign the value 101“) To increment or decrement the value at the location pointed to by a pointer, you can use a statement like this: (*p)++;

Assigning Values Through a Pointer(2) The following program demonstrates assignment using a pointer. 1.#include 2.using namespace std; 3.int main() 4.{ 5.int *p, num; 6.p = &num; 7.*p = 100; 8.cout << num << ' '; 9.(*p)++; 10.cout << num << ' '; 11.(*p)--; 12.cout << num << '\n'; 13.return 0; 14.}

The output from the program is shown here

Pointer Expressions Pointers can be used in most valid C++ expressions. Remember also that you may need to surround some parts of a pointer expression with parentheses

Pointer Arithmetic There are only four arithmetic operators that can be used on pointers: ++, – –, +, and –. Let p1 be an integer pointer with a current value of 2,000. After the expression p1++; the contents of p1 will be 2,004, not 2,001! Each time that a pointer is incremented, it will point to the memory location of the next element of its base type.

Pointer Arithmetic(2) You can also add or subtract integers to or from pointers. The expression p1 = p1 + 9; makes p1 point to the ninth element of p1’s base type, beyond the one to which it is currently pointing. To see the effects of pointer arithmetic, execute the next short program.

1.// Demonstrate pointer arithmetic. 2.#include 3.using namespace std; 4.int main() 5.{ 6.int *i, j[10]; 7.double *f, g[10]; 8.int x; 9.i = j; 10.f = g; 11.for(x=0; x<10; x++) 12.cout << i+x << ' ' << f+x << '\n'; 13.return 0; 14.}

Here is sample output. 0012FE5C 0012FE FE FE8C 0012FE FE FE FE9C 0012FE6C 0012FEA4 0012FE FEAC 0012FE FEB4 0012FE FEBC 0012FE7C 0012FEC4 0012FE FECC

Pointer Comparisons Pointers may be compared by using relational operators, such as ==,. If p1 and p2 are pointers that point to two separate and unrelated variables, then any comparison between p1 and p2 is generally meaningless. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.

Pointers and Arrays In C++, there is a close relationship between pointers and arrays. In fact, frequently a pointer and an array are interchangeable. consider this fragment: char str[80]; char *p1; p1 = str; if you want to access the fifth element in str, you could use:- str[4] or *(p1+4)

Pointers and Arrays(2) C++ allows two methods of accessing array elements: pointer arithmetic and array indexing. Pointer arithmetic can sometimes be faster than array indexing. Example: two versions of the same program will be shown next.

1.// Tokenizing program: pointer version. 2.#include 3.#include 4.using namespace std; 5.int main() 6.{ 7.char str[80]; 8.char token[80]; 9.char *p, *q; 10.cout << "Enter a sentence: "; 11.gets(str); 12.p = str; 13.// Read a token at a time from the string. 14.while(*p) { 15.q = token; // set q pointing to start of token 16./* Read characters until either a space or the 17.null terminator is encountered. */ 18.while(*p!=' ' && *p) { 19.*q = *p; 20.q++; p++; 21.} 22.if(*p) p++; // advance past the space 23.*q = '\0'; // null terminate the token 24.cout << token << '\n'; 25.} 26.return 0; 27.} 28.Here

1.// Tokenizing program: array-indexing version. 2.#include 3.#include 4.using namespace std; 5.int main() 6.{ 7.char str[80]; 8.char token[80]; 9.int i, j; 10.cout << "Enter a sentence: "; 11.gets(str); 12.// Read a token at a time from the string. 13.for(i=0; ; i++) { 14./* Read characters until either a space or the 15.null terminator is encountered. */ 16.for(j=0; str[i]!=' ' && str[i]; j++, i++) 17.token[j] = str[i]; 18.token[j] = '\0'; // null terminate the token 19.cout << token << '\n'; 20.if(!str[i]) break; 21.} 22.return 0; 23.}

Indexing a Pointer It is possible to index a pointer as if it were an array. 1.// Indexing a pointer like an array. 2.#include 3.#include 4.using namespace std; 5.int main() 6.{ 7.char str[20] = "hello tom"; 8.char *p; 9.int i; 10.p = str; // put address of str into p 11.// now, index p like an array 12.for(i=0; p[i]; i++) p[i] = toupper(p[i]); 13.cout << p; // display the string 14.return 0; 15.}

The program displays HELLO TOM Remember, the expression p[i] is functionally identical to *(p+i).

Are Pointers and Arrays Interchangeable? Pointers and arrays are interchangeable in many cases, but not completely. Consider this fragment: int num[10]; int i; for(i=0; i<10; i++) { *num = i; // this is OK num++; // ERROR -- cannot modify num }

Pointers and String Literals The following program demonstrate how pointer is used with string literals. #include using namespace std; int main() { char *s; s = "Pointers are fun to use.\n"; cout << s; return 0; }

A Comparison Example It is legal to compare the value of one pointer to another (which is point to elements of the same array). The following program demonstrates a pointer comparison. // A pointer comparison example. #include using namespace std; int main() { int num[10]; int *start, *end; start = num; end = &num[9];

// enter the values while(start <= end) { cout << "Enter a number: "; cin >> *start; start++; } start = num; // reset the starting pointer // display the values while(start <= end) { cout << *start << ' '; start++; } return 0; }

Arrays of Pointers Pointers can be arrayed like any other data type. For example, int *ipa[10]; //each element in ipa holds a pointer to an int value. To assign the address of an int variable called var to the third element of ipa ipa[2] = &var; Using the ipa array to assign the value of var to an int variable called x x = *ipa[2];

Arrays of Pointers(2) Like other arrays, arrays of pointers can be initialized. char *fortunes[] = { "Soon, you will come into some money.\n", "A new love will enter your life.\n", "You will live long and prosper.\n", "Now is a good time to invest for the future.\n", "A close friend will ask for a favor.\n" }; Thus, to print the second message, use a statement like this: cout << fortunes[1];

The Null Pointer Convention Any type of pointer should be initialized to null when it is declared. For example, the following initializes p to null: float *p = 0; // p is now a null pointer If you follow the null pointer convention, you will avoid many problems when using pointers.

Problems with Pointers When a pointer accidentally contains the wrong value, it can be the most difficult bug to track down. The classic example of a pointer error is the uninitialized pointer. Consider this example: // This program is wrong. int main(){ int x, *p; x = 10; *p = x; // where does p point? return 0; } //You will have no way of knowing where the value of x has been written.

Passing Arguments to Functions by Reference with Pointers There are three ways in C++ to pass arguments to a function:- 1.pass-by-value 2.pass-by-reference (with pointer) Figure 8.6 and Figure 8.7 present two versions of a function that cubes an integercubeByValue and cubeByReference

Cube a variable using pass-by-value. 2 // Cube a variable using pass-by-value. 3 #include 4 using std::cout; 5 using std::endl; 6 7 int cubeByValue( int ); // prototype 8 9 int main() 10 { 11 int number = 5; cout << "The original value of number is " << number; number = cubeByValue( number ); // pass number by value to cubeByValue 16 cout << "\nThe new value of number is " << number << endl; 17 return 0; // indicates successful termination 18 } // end main 19

Cube a variable using pass-by-value.(cont) 20 // calculate and return cube of integer argument 21 int cubeByValue( int n ) 22 { 23 return n * n * n; // cube local variable n and return result 24 } // end function cubeByValue Output The original value of number is 5 The new value of number is 125

Cube a variable using pass-by-reference with a pointer argument. 2 // Cube a variable using pass-by-reference with a pointer argument. 3 #include 4 using std::cout; 5 using std::endl; 6 7 void cubeByReference( int * ); // prototype 8 9 int main() 10 { 11 int number = 5; cout << "The original value of number is " << number; cubeByReference( &number ); // pass number address to cubeByReference cout << "\nThe new value of number is " << number << endl; 18 return 0; // indicates successful termination 19 } // end main

Cube a variable using pass-by-reference with a pointer argument(cont) 21 // calculate cube of *nPtr; modifies variable number in main 22 void cubeByReference( int *nPtr ) 23 { 24 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr 25 } // end function cubeByReference Output The original value of number is 5 The new value of number is 125

Calling Functions with Arrays When an array is an argument to a function, only the address of the first element of the array is passed. Remember- in C++, an array name without an index is a pointer to the first element in the array.) Examine the following example. To call display( ), pass the address of the array as the first argument, and the size of the array as the second.

#include using namespace std; //void display(int *, int ); void display(int *num,int n) { int i; for(i=0; i<n; i++) cout << num[i] << ' '; } int main() { int t[10],i; for(i=0; i<10; ++i) t[i]=i; display(t,10); // pass array t and it size to a function return 0; }

Summary Pointers are variables that contain as their values memory addresses of other variables. The declarationint *ptr; declares ptr to be a pointer to a variable of type int and is read, "ptr is a pointer to int." The * as used here in a declaration indicates that the variable is a pointer. There are three values that can be used to initialize a pointer: 0, NULL or an address of an object of the same type. Initializing a pointer to 0 and initializing that same pointer to NULL are identical0 is the convention in C++.

Summary(2) The & (address) operator returns the memory address of its operand. A function receiving an address as an argument must have a pointer as its corresponding parameter. There are four ways to pass a pointer to a functiona nonconstant pointer to nonconstant data, a nonconstant pointer to constant data, a constant pointer to nonconstant data and a constant pointer to constant data. The value of the array name is the address of (a pointer to) the array's first element.

Summary(3) To pass a single element of an array by reference using pointers, pass the address of the specific array element. The arithmetic operations that may be performed on pointers are incrementing (++) a pointer, decrementing (--) a pointer, adding (+ or +=) an integer to a pointer, subtracting (- or -=) an integer from a pointer and subtracting one pointer from another. When an integer is added or subtracted from a pointer, the pointer is incremented or decremented by that integer times the size of the object to which the pointer refers.

Summary(4) Pointers can be compared using the equality and relational operators. Comparisons using relational operators are meaningful only if the pointers point to members of the same array. Pointers that point to arrays can be subscripted exactly as array names can.