Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and Pointer-Based Strings

Similar presentations


Presentation on theme: "Pointers and Pointer-Based Strings"— Presentation transcript:

1 Pointers and Pointer-Based Strings
CHAPTER 3 Pointers and Pointer-Based Strings

2 Introduction to Pointers
Pointers are the powerful feature of C++ programming  . To understand pointers, you should have the knowledge of address in computer memory Computer memory is broken down into bytes and each byte has its own address . For example: In 1KB memory, there are 1024 bytes and each byte is given an address ( ).

3 Introduction to Pointers
Pointer variables contain memory addresses as their values. Normally, a variable directly contains a specific value. However, a pointer contains the memory address of a variable that in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value.

4 Pointer Operators The address operator (&) is a unary operator that returns the memory address of its operand. The & operator can find address occupied by a variable. If var is a variable then, &var gives the address of that variable.

5 Pointer Variables Pointer variables or simply pointers are the special types of variable that holds memory address instead of data. How to declare a pointer? int *p; The statement above defines a pointer variable p. The pointer p holds the memory address. The asterisk is a dereference operator which means pointer to.  Here pointer p is a pointer to int, that is, it is pointing an integer.

6 Example Program #include <iostream> using namespace std; int main() { int *pc, c; c = 5; cout<< "Address of c (&c): " << &c << endl; cout<< "Value of c (c): " << c << endl << endl; pc = &c; // Pointer pc holds the memory address of variable c cout<< "Address that pointer pc holds (pc): "<< pc << endl; cout<< "Content of the address pointer pc holds (*pc): " << *pc << endl << endl; return 0; }

7 Output: Address of c (&c): 0x7fff5fbff80c Value of c (c): 5 Address that pointer pc holds (pc): 0x7fff5fbff80c Content of the address pointer pc holds (*pc): 5

8 Passing arguments to a function
There are 3 ways in C++ to pass arguments to a function: Pass-by-value Pass-by-reference with reference arguments Pass-by-reference with pointer arguments Note: Pass-by-value and Pass-by-reference with reference arguments we already discussed in chapter 2.

9 Pass-by-reference with pointer arguments
There is another way of passing argument in which actual value is not passed, only the reference to that value is passed. This method is called pass-by-reference with pointer arguments.

10 Example Program #include <iostream> using namespace std; int main() { void swap(int*, int*); // Function prototype int a = 1, b = 2; cout << "Before swaping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(&a, &b);

11 Example Program Before swapping a = 1 b = 2 After swapping a = 2 b = 1
cout << "\nAfter swaping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; } void swap(int* n1, int* n2) { int temp; temp = *n1; *n1 = *n2; *n2 = temp; Output: Before swapping a = 1 b = 2 After swapping a = 2 b = 1

12 Program Explanation In this , the address of variable is passed during function call rather than variable itself. swap(&a, &b); // &a is address of a and &b is address of b Since the address is passed instead of value, dereference operator must be used to access the value stored in that address. void swap(int* n1, int* n2) { } The *n1 and *n2 gives the value stored at address n1 and n2 respectively.

13 Using const with Pointers
 The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified. If a value does not change in the body of a function to which it is passed, the parameter should be declared const to ensure that it is not accidentally modified. If an attempt is made to modify a const value, a warning or an error is issued, depending on the particular compiler.

14 Using const with Pointers cont..
 There are 4 ways to pass a pointer to a function: A non-constant pointer to non-constant data A non-constant pointer to constant data A constant pointer to non-constant data A constant pointer to constant data

15 sizeof operator The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. The syntax of using sizeof is as follows: sizeof (data type) Where data type is the desired data type including classes, structures, unions and any other user defined data type.

16 Relationship between Pointer & Arrays
Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. Consider the following program: #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0;

17 Pointers and Arrays Pointers are the variables that hold address. Pointers can point at cells of an array not only single variable. Consider this example: int* ptr; int a[5]; ptr = &a[2]; // &a[2] is the address of third element of a[5].

18 Pointers and Arrays You know that, pointer ptr holds the address and expression *ptr gives the content in that address. Similarly, *(ptr + 1) gives the content in address ptr + 1. Consider this code below: int p[5] = {3, 4, 5, 5, 3}; &p[0] is equal to p and *p is equal to p[0] &p[1] is equal to p+1 and *(p+1) is equal to p[1] &p[2] is equal to p+2 and *(p+2) is equal to p[2] &p[i] is equal to p+i and *(p+i) is equal to p[i]


Download ppt "Pointers and Pointer-Based Strings"

Similar presentations


Ads by Google