Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming fundamentals 2 Chapter 2:Pointer

Similar presentations


Presentation on theme: "Programming fundamentals 2 Chapter 2:Pointer"— Presentation transcript:

1 Programming fundamentals 2 Chapter 2:Pointer
Miss:Hanan Hardam

2 Pointers in C++ Topics to cover: Overview of Pointers
Pointer Declaration Pointer Assignment Pointer Arithmetic

3 Overview of Pointers A Pointer in C++ is variable whose value is a memory address. With pointers many memory locations can be referenced. Some data structures use pointers (e.g. linked list, tree). The * and & operators - & operator is the address operator - * operator is the dereferencing operator. It is used in pointers declaration

4 Pointer Declaration Pointers are declared as follows:
<type> * variable_name ; e.g. int * xPtr; // xPtr is a pointer to data of type integer char * cPtr; //cPtr is a pointer to data of type character void * yPtr; // yPtr is a generic pointer, // represents any type

5 Pointer Assignment Assignment can be applied on pointers of the same type If not the same type, a cast operator must be used Exception: pointer to void does not need casting to convert a pointer to void type void pointers cannot be dereferenced Example int *xPtr, *yPtr; int x = 5; xPtr = & x; // xPtr now points to address of x yPtr = xPtr; // now yPtr and xPtr point to x

6 Cont. Example int *xPtr, *yPtr; int x = 5; xPtr = & x; yPtr = xPtr;

7 Cont. Example 4. cout << *xPtr; // this statement should print the value that is pointed to by xPtr, which is cout<< xPtr; // this statement should print the contents of xPtr location, which is the address The same applies for yPtr, since it points to the same location as xPtr.

8 Pointer Arithmetic Increment / decrement pointers ( ++ or -- )
Add / subtract an integer to/from a pointer ( + or += , - or -= ) Pointers may be subtracted from each other Pointer arithmetic is meaningless unless performed on an array This will be discussed in more details in Arrays chapter.

9 Examples on Pointers //File: pointers.cpp
//A program to test pointers and references #include <iostream.h> void main ( ) { int intVar = 10; int *intPtr; // intPtr is a pointer intPtr = & intVar; cout << "\nLocation of intVar: " << & intVar; cout << "\nContents of intVar: " << intVar; cout << "\nLocation of intPtr: " << & intPtr; cout << "\nContents of intPtr: " << intPtr; cout << "\nThe value that intPtr points to: " << * intPtr; }

10 Call by Reference #include <iostream.h> void Increment(int&);
void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; }

11 Call by Reference #include <iostream.h> void Increment(int&);
//if you add extra cout statements to the program, the output will become: #include <iostream.h> void Increment(int&); void main() { int A = 10; cout << “in main “<< &A << endl; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; cout << “in fun “<< X << endl; cout << “in fun “<< &X << endl;

12 Call by Pointer #include <iostream.h> void Increment(int*);
void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; }

13 Call by Pointer //if you add extra cout statements to the program, the output will become: #include <iostream.h> void Increment(int*); void main() { int A = 10; cout << "in main "<< &A<< endl; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; cout << "in fun X "<< X << endl; cout << "in fun *X "<< *X << endl; cout << "in fun &X"<< &X <<endl;

14 Examples on Pointers (Cont.)
//File: swap.cpp //A program to call a function to swap two numbers using reference parameters  #include <iostream.h> void swap(int *, int *); // This is swap's prototype void main() { int x = 5, y = 7; swap(&x , &y); // calling swap with reference parameters cout << "\n x is now "<< x << " and y is now " << y << '\n'; } // swap function is defined here using dereferencing operator ‘*’ void swap(int *a, int *b) { int temp; temp = *a; // here the values which a and b point to are swapped *a = *b; *b = temp;


Download ppt "Programming fundamentals 2 Chapter 2:Pointer"

Similar presentations


Ads by Google