Pointer in C++ - PowerPoint PPT Presentation

About This Presentation
Title:

Pointer in C++

Description:

All About Pointer(Definition ,Declaring Pointer Variable, &(‘Address of’ Operator), *(‘Value of’ or Indirection Operator), Pointers Type ,Pointer Arithmetic ,Pass by Pointer in function,Array of Pointer, String of Pointer,Object of Pointer,this Pointer ) with syntax and example. – PowerPoint PPT presentation

Number of Views:1598
Updated: 23 March 2017
Slides: 30
Provided by: surajmaurya015
Tags: c

less

Transcript and Presenter's Notes

Title: Pointer in C++


1
POINTERS IN C
POINTERS IN C
2
Present by -
Suraj R. Maurya
3
  • Topics
  • All About Pointers
  • Definition
  • Declaring Pointer Variable
  • (Address of Operator)
  • (Value of or Indirection Operator)
  • Pointers Type
  • Pointer Arithmetic
  • Pass by Pointer in function

4
  • Array of Pointer
  • String of Pointer
  • Object of Pointer
  • this Pointer
  • Advantages of Pointer
  • Limitations of Pointer

5
Computer Memory
  • Each variable is assigned a memory slot (the size
    depends on the data type) and the variables data
    is stored there
  • Eg int a100



Memory address
1024
1032
1020
100


1024

A
Variable as value, i.e., 100, is stored at
memory location 1024
6
  • Every byte in computer memory has an address.
  • Addresses are numbers just like house numbers.
  • Addresses start from 0 and go up as 1, 2, 3.e.g.
    in 640 KB memory addresses will be 0 to 655359
    I.e., (640 X 1024)-1.
  • The moment a variable is declared, memory is
    allocated in the RAM for its storage. int X100.
  • A variable in the RAM can be accessed only if you
    know its address.

7
Definition
  • A pointer is a variable which store the memory
    address of another variable.
  • It supports dynamic memory allocation routines.
  • A pointer variable and normal variable is always
    same data type.

8
Declaration And Initialization Of Pointer
Variable
  • Syntax
  • Datatype variable_name
  • eg. int x float y char z
  • Address of operator()- it is a unary operator
    that returns the memory address of its operand.
    Here the operand is a normal variable.
  • Ptr_namevariable_name

9
  • eg. int x 10
  • int ptr
  • ptr x
  • Now ptr will contain address
  • where the variable x is stored
  • in memory

10
(Value of or Indirection Operator)
  • It is a unary operator that returns the value
    stored at the address pointed to by the pointer.
  • Here the operand is a pointer variable.
  • eg.
  • int x 10
  • int ptr x
  • coutltlt ptr// address stored at ptr will be
    displayed
  • coutltltptr// value pointed to by ptr will be
    displayed
  • Now ptr can also be used to change/display the
    value of x.


11
Pointers Type

12


13
Pointer Arithmetic
  • Two arithmetic operations, addition and
    subtraction, may be performed on pointers. When
    we add 1 to a pointer, we are actually adding the
    size of data type in bytes, the pointer is
    pointing at.
  • For e.g. int x x
  • If current address of x is 1000, then x
    statement will increase x by 2(size of int data
    type) and makes it 1002, not 1001.


14
  • Consider p1 and p2 as pointers
  • P1ap2b
  • The arithmethic operations that can be performed
    on p1 and p2 are summarized in the table below

15
Pointer Expressions
  • A Pointer expression contains pointer variable
  • as an operand and arithmetic operator such
  • as ,-,,/
  • Eg (p1) This statement increments value,
    stored at the memory address pointed by pointer
    p1 by 1.

16
Pass by Pointer in function
  • Consider the program of swapping two
    variables with the help of pointers
  • includeltiostream.hgt
  • void swap(int m, int n)
  • int temp temp m m n n temp
  • void main()
  • int a 5, b 6
  • cout ltlt \n Value of a ltlt a ltlt and b ltlt
    b
  • swap(a, b)
  • cout ltlt \n After swapping value of a ltlt a ltlt
    and b ltlt b

17
COMPARING PASS BY VALUE, PASS BY REFERENCE AND
PASS BY POINTER
PASS BY VALUE PASS BY REFERENCE PASS BY POINTER
Separate memory is allocated to formal parameters. Formal and actual parameters share the same memory space. Formal parameters contain the address of actual parameters.
Changes done in formal parameters are not reflected in actual parameters. Changes done in formal parameters are reflected in actual parameters. Changes done in formal parameters are reflected in actual parameters.
For eg. void cube(int x) x xxx void main() int y10 coutltltyltltendl cube(y) coutltltyltltendl output 1010 For eg. void cube(int x) x xxx void main() int y10 coutltltyltltendl cube(y) coutltltyltltendl output 101000 For eg. void cube(int x) x (x)(x)(x) void main() int y10 coutltltyltltendl cube(y) coutltltyltltendl output 101000
18
Array of Pointer
  • Array is collection of similar data type.
  • We can create pointer variable with array both
    are similar data type.
  • Using array of pointer searching, insertion,
    deletion etc. operation we can perform.
  • Syntax
  • Data_type variable_namesize of array
  • Data_type pointer_variable
  • pointer_variablevariable_name
  • OR
  • pointer_variablevariable_name0

19
  • Example of pointer of array
  • Void main()
  • int a2,ptr
  • Ptra0
  • CoutltltPtr ltltptr
  • Ptr

20
String of Pointer
  • Array of character is called string.
  • We can create pointer variable with string both
    are similar data type.
  • Using string of pointer searching, finding length
    , comparisons ,concatenation ,reverse etc
    operation we can perform.
  • Syntax
  • Data_type variable_namesize of string
  • Data_type pointer_variable
  • Pointer_variablevariable_name0

21
  • Example of string of pointer
  • void main()
  • Char name4NEEL,ptr
  • Ptrname0
  • While(ptr!\0)
  • Ptr
  • Coutltltptr

22
Object of Pointer
  • When address of an object of a class is stored
    into the pointer variable of the same class type
    then it is pointer of object.
  • This pointer can be used to access the data
    member and member function of same class.

23
Syntax
  • Class_name object_name
  • Class_name pointer_variable
  • Pointer_variableobject_name
  • Pointer_variable-gtfunction_name()

24
Example of pointer to object
  • //create a class syco
  • //and declare data member and function
  • //Now, in main function
  • Syco obj
  • Syco ptr
  • Ptrobj
  • Obj-gtgetdtat()
  • Obj-gtputdata()

25
this Pointer
  • C uses a unique keyword called this to
    represent an object that invokes a member
    function.
  • This is a pointer that points to the object for
    which this function was called.
  • This is a unique pointer is automatically passed
    to a member function when it is called.
  • The pointer this acts as an implicit argument to
    all the member function.
  • its an in-built pointer so programmer doesnt
    need to explicitly create this pointer.

26
  • One class can have exactly one this pointer.
  • Syntax
  • There are two ways of accessing member with
    this pointer as follows
  • this-gtdata_member
  • this-gtmember_function()
  • OR
  • this(dtat_member)

27
Example of this pointer
  • Class abc
  • Int a
  • Public
  • Void getdata()
  • The private variable a can be used directly
    inside a member function, like a123
  • But in this concept(in main function) to do same
    job
  • This-gta123
  • This -gtgetdata()

28
Advantages of Pointer
  1. Pointer can handle array more efficiently.
  2. As they provide direct access to memory, they
    process data very fast.
  3. They reduce storage space.
  4. They give alternate way to access elements of an
    array.
  5. Pointers avoid any confusion by the compiler due
    to variables with same name.
  6. They help in building complex data structures
    like linked list , stacks , queues , trees ,
    graphs etc.

29
Limitations of Pointer
  1. They are slower than normal variables.
  2. Pointers store only address but cannot store a
    value.
  3. It requires a null pointer reference.
  4. If a pointer is initialized with incorrect value
    , it can lead to a garbage value which may cause
    error in the program.
Write a Comment
User Comments (0)
About PowerShow.com