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

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

Embed Size (px)

DESCRIPTION

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;

Citation preview

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

DCT1063 Programming 2

CHAPTER 1 POINTERS

Mohd Nazri Bin IbrahimFaculty of Computer, Media & Technology

TATi University [email protected]

Page 2: 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:-

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

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;

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

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.

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

• Consider the following program:-1. #include <iostream>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. }

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

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

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

The Pointer Operators(2)• What is the output of the program above?

Balance is : 3200

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

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

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

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:

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

1. // This program will not work right.2. #include <iostream>3. using namespace std;4. int main()5. {6. double x, y;7. int *p;8. x = 123.23;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.}

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

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)++;

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

Assigning Values Through a Pointer(2)

• The following program demonstrates assignment using a pointer.1. #include <iostream>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. }

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

• The output from the program is shown here.

100 101 100

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

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

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

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.

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

Pointer Arithmetic(2)• You can also add or subtract integers to or

from pointers. The expressionp1 = 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.

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

1. // Demonstrate pointer arithmetic.2. #include <iostream>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. }

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

• Here is sample output. 0012FE5C 0012FE84

0012FE60 0012FE8C0012FE64 0012FE940012FE68 0012FE9C0012FE6C 0012FEA40012FE70 0012FEAC0012FE74 0012FEB40012FE78 0012FEBC0012FE7C 0012FEC40012FE80 0012FECC

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

Pointer Comparisons• Pointers may be compared by using relational

operators, such as ==, <, and >.• 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.

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

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)

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

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.

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

1. // Tokenizing program: pointer version.2. #include <iostream>3. #include <cstdio>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 token16. /* Read characters until either a space or the17. 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

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

1. // Tokenizing program: array-indexing version.

2. #include <iostream>3. #include <cstdio>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 the15.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 token19. cout << token << '\n';20. if(!str[i]) break;21. }22. return 0;23. }

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

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 <iostream>3. #include <cctype>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 p11. // now, index p like an array12. for(i=0; p[i]; i++) p[i] = toupper(p[i]);13.cout << p; // display the string14. return 0;15. }

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

• The program displaysHELLO TOM

• Remember, the expression p[i] is functionally identical to *(p+i).

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

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 OKnum++; // ERROR -- cannot modify num}

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

Pointers and String Literals• The following program demonstrate how

pointer is used with string literals. #include <iostream>using namespace std;int main(){char *s;s = "Pointers are fun to use.\n";cout << s;return 0;}

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

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 <iostream>using namespace std;int main(){int num[10];int *start, *end;start = num;end = &num[9];

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

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

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

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];

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

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];

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

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.

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

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.

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

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

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

Cube a variable using pass-by-value.

2 // Cube a variable using pass-by-value. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int cubeByValue( int ); // prototype 8 9 int main()10 {11 int number = 5;1213 cout << "The original value of number is " << number;1415 number = cubeByValue( number ); // pass number by value

to cubeByValue16 cout << "\nThe new value of number is " << number <<

endl;17 return 0; // indicates successful termination18 } // end main19

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

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

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

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 <iostream> 4 using std::cout; 5 using std::endl; 6 7 void cubeByReference( int * ); // prototype 8 9 int main()10 {11 int number = 5;1213 cout << "The original value of number is " << number;1415 cubeByReference( &number ); // pass number address to

cubeByReference1617 cout << "\nThe new value of number is " << number <<

endl;18 return 0; // indicates successful termination19 } // end main

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

Cube a variable using pass-by-reference with a pointer argument(cont)

21 // calculate cube of *nPtr; modifies variable number in main22 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

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

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.

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

#include <iostream>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 functionreturn 0;}

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

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++.

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

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.

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

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.

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

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.