Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.

Similar presentations


Presentation on theme: "Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered."— Presentation transcript:

1 Pointers 1

2 Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered

3 Pointers in C++ is a very powerful programming technique. It is very useful in: ◦ To call functions using “Pass-by-Reference” technique ◦ Dynamic memory ◦ Improve the efficiency of functions ◦ Develop dynamic data structures, such as binary trees and linked lists etc. 3 Introduction

4 A pointer is a variable that holds a memory address. This address is the location of another variable in memory. 4 Introduction contd. 10 int count 0X10012 Variable Name Memory Address Value Stored at the Memory address 0X10012 int *count_ptr 0X10014 A normal variable A pointer variable

5 In spite of the powerfulness pointers are very dangerous as well. ◦ Programs with pointers can go wrong very easily. ◦ Programmers often make lot of mistakes in manipulating pointers. Due to this reason new languages such as Java had eliminated ‘Pointer’ concept altogether. 5 Introduction contd.

6 Like the normal variables, pointer variables must be declared before use. Pointers are denoted using the sign * Examples: ◦ int *count_ptr – A pointer to an integer variable ◦ float *avg_ptr – A pointer to a floating point variable ◦ char *choice_ptr - A pointer to a character variable 6 Declaring a Pointer Variable

7 Initially when you declare a pointer variable it will be NULL ◦ The pointer dose not point to any object. 7 Declaring a Pointer Variable contd. NULL int *count_ptr 0X10014 Variable Name Memory Address Value Stored at the Memory address

8 There are two types of pointer operators. 8 Pointer Operators OperatorMeaningDescription &Address of operator Returns the memory address of a variable *Dereferencing operator Returns the value stored at the location pointed by a pointer

9 Example 1.int marks; 2.int *marks_ptr; 3.marks = 30; 4.marks_ptr = &marks; We can use & operator to initialize a pointer variable 9 The Address of Operator - & int marks 0X10034 int *marks_ptr 0X10036 0X10034 30

10 Example 1.int marks, value; 2.int *marks_ptr; 3.marks = 30; 4.marks_ptr = &marks; 5.value = *marks_ptr; 10 The Dereferencing Operator - * int marks 0X10034 int *marks_ptr 0X10038 0X10034 30 int value 0X10036 30

11 The Dereferencing Operator is useful when we want to access and modify the actual content of the variable which is pointed by a pointer. 11 The Dereferencing Operator contd.

12 What is the result of the following code ? int number; int *number_ptr; number = 30; number_ptr = &number; *number_ptr = 50; cout<<“number = ”<<number”; 12 Pointer Operators - Exercise number = 50

13 What is the result of the following code ? int number; int *number1_ptr; int *number2_ptr; number = 30; number1_ptr = &number; number2_ptr = number1_ptr; *number2_ptr = 10; cout<<"number =“<<number; 13 Pointer Operators - Exercise number = 10

14 What is the result of the following code ? int number1 = 10; int number2 = 20; int *number1_ptr; int *number2_ptr; number1_ptr = &number1; number2_ptr = &number2; *number2_ptr = *number1_ptr; * number1_ptr = 50; cout<<"number1 = “<<number1; cout<<”number2 = “<<number2; 14 Pointer Operators - Exercise number1 = 50 number2 = 10

15 Thank you. 15


Download ppt "Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered."

Similar presentations


Ads by Google