intro to pointer c++

16
C++ [ Pointers ] Eng. Ahmed Farag

Upload: ahmed-farag

Post on 15-Jan-2017

516 views

Category:

Education


0 download

TRANSCRIPT

Page 1: intro to  pointer C++

C++ [ Pointers ]

Eng. Ahmed Farag

Page 2: intro to  pointer C++

Pointers As you know every variable is a memory

location and every memory location has its

address defined which can be accessed using

ampersand (&) operator which denotes an address in memory.

Page 3: intro to  pointer C++

Example1. #include <iostream>2. using namespace std;

3. int main ()4. { int var1;5. char var2[10];6. cout << "Address of var1 variable: ";7. cout << &var1 << endl;

8. cout << "Address of var2 variable: ";9. cout << &var2 << endl;10. return 0;11. }

Address of var1 variable: 0xbfebd5c0Address of var2 variable: 0xbfebd5b6

Page 4: intro to  pointer C++

What Are Pointers? A pointer is a variable whose value is the

address of another variable.

Like any variable or constant, you must declare a pointer before you can work with it.

The general form of a pointer variable declaration is: type *var-name;

Page 5: intro to  pointer C++

Examples int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character

Page 6: intro to  pointer C++

Example There are few important operations, which we will do

with the pointers very frequently. 

1. we define a pointer variables 

2. Assign the address of a variable to a pointer  

3. Finally access the value at the address available in the pointer variable. 

This is done by using unary operator * that returns the value of the variable located at the address specified by its operand

Page 7: intro to  pointer C++

Example Con… 1. #include <iostream>2. using namespace std;

3. int main ()4. { int var = 20; // actual variable declaration.5. int *ip; // pointer variable 6. ip = &var; // store address of var in pointer variable7. cout << "Value of var variable: ";8. cout << var << endl;9. // print the address stored in ip pointer variable10. cout << "Address stored in ip variable: ";11. cout << ip << endl;12. // access the value at the address available in pointer13. cout << "Value of *ip variable: ";14. cout << *ip << endl;15. return 0;16. }

Value of var variable: 20Address stored in ip variable: 0xbfc601acValue of *ip variable: 20

Page 8: intro to  pointer C++

Pointer arithmetic As you understood pointer is an address which

is a numeric value; therefore, you can perform

arithmetic operations on a pointer just as you can a numeric value.

There are four arithmetic operators that can be used on

pointers: ++, --, +, and -

Page 9: intro to  pointer C++

Incrementing a Pointer We prefer using a pointer in our program instead of an

array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer. 

Page 10: intro to  pointer C++

1. #include <iostream>2. using namespace std;3. const int MAX = 3;

4. int main ()5. { int var[MAX] = {10, 100, 200};6. int *ptr;7. // let us have array address in pointer.8. ptr = var;9. for (int i = 0; i < MAX; i++)10. { cout << "Address of var[" << i << "] = ";11. cout << ptr << endl;12. cout << "Value of var[" << i << "] = ";13. cout << *ptr << endl;14. // point to the next location15. ptr++;16. }17. return 0;18. }

Address of var[0] = 0xbfa088b0Value of var[0] = 10Address of var[1] = 0xbfa088b4Value of var[1] = 100Address of var[2] = 0xbfa088b8Value of var[2] = 200

Page 11: intro to  pointer C++

1. #include <iostream>2. using namespace std;3. const int MAX = 3;

4. int main ()5. { int var[MAX] = {10, 100, 200};6. int *ptr;7. // let us have address of the last element in pointer.8. ptr = &var[MAX-1];9. for (int i = MAX; i > 0; i--)10. { cout << "Address of var[" << i << "] = ";11. cout << ptr << endl;12. cout << "Value of var[" << i << "] = ";13. cout << *ptr << endl;14. // point to the previous location15. ptr--;16. }17. return 0;18. }

Address of var[3] = 0xbfdb70f8Value of var[3] = 200Address of var[2] = 0xbfdb70f4Value of var[2] = 100Address of var[1] = 0xbfdb70f0Value of var[1] = 10

Page 12: intro to  pointer C++

Pointers vs Arrays Pointers and arrays are strongly related. In fact, pointers

and arrays are interchangeable in many cases.

For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing. 

Page 13: intro to  pointer C++

1. #include <iostream>2. using namespace std;3. const int MAX = 3;4. 5. int main ()6. { int var[MAX] = {10, 100, 200};7. int *ptr;8. // let us have array address in pointer.9. ptr = var;10. for (int i = 0; i < MAX; i++)11. {12. cout << "Address of var[" << i << "] = ";13. cout << ptr << endl;14. cout << "Value of var[" << i << "] = ";15. cout << *ptr << endl;16. // point to the next location17. ptr++;18. }19. return 0;20. }

Address of var[0] = 0xbfa088b0Value of var[0] = 10Address of var[1] = 0xbfa088b4Value of var[1] = 100Address of var[2] = 0xbfa088b8Value of var[2] = 200

Page 14: intro to  pointer C++

Pointers vs Arrays Con… However, pointers and arrays are not completely

interchangeable.

Page 15: intro to  pointer C++

Pointers vs Arrays Con …1. #include <iostream>2. using namespace std;3. const int MAX = 3;4. 5. int main ()6. {7. int var[MAX] = {10, 100, 200};8. for (int i = 0; i < MAX; i++)9. {10. *var = i; // This is a correct syntax

11. var++; // This is incorrect.12. }13. return 0;14. }

Page 16: intro to  pointer C++

Array of pointers file:///D:/1-%20Computer%20Science%20Matirial/Offline

%20Sites/Tutorialspoint/C++%20tutorialspoint/cpp_array_of_pointers.htm