BIRLA INSTITUTE OF TECHNOLOGY TOPIC :- POINTERS BASICS, CONSTANT POINTERS AND POINTERS TO CONSTANT DIVYANSH PARMARTHI MCA/25016/18 SHRADDHA
POINTERS • A pointer is a variable that points a address of a variable. • A pointer is declared using the * operator before an identifier.
 Declaration of Pointer variables : type *pointer_name; where type is the type of data pointed to For example :- int *numberPointer;
ADDRESS (&) OPERATOR  Pointer address operator is denoted by ‘&’ symbol  The & is a unary operator that returns the memory address of its operand. Example : &n - It gives an address on variable n
DEREFERENCING OPERATOR (*)  We can access to the value stored in the variable pointed to by using the dereferencing operator (*) Syntax: *PointerVariable Example: int V = 101; int *P = &V;
POINTERS TO POINTERS  A pointer to a pointer is a chain of pointers.  In pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value. Pointer Pointer Variable Address Address Value
Declaration of pointer to a pointer : int **ptr; For example : // local declaration int a; int *p; int **q; // Statment a=58; p=&a; q=&p;
cout << a << “ “ <<endl; cout << *p << “ “ <<endl; cout<< **q << “ “ <<endl; OUTPUT 58 58 58
POINTERS AS PARAMETERS  To make changes to a variable that exist after a function ends, we pass the address of the variable to the function For example : #include<iostream.h> void Indirectswap(char *ptr1, char *ptr2) { char c = *ptr1; *ptr1 = *ptr2; *ptr2 = c; }
int main() { char a = ‘y’; char b = ‘n’; Indirectswap(&a,&b); cout<< a << “and”<< b << endl; return 0; } OUTPUT n and y
CONSTANT POINTERS AND POINTERS TO CONSTANT BY :- SHRADDHA MALVIYA
Constant pointer  Constant pointer cannot be modified.  Modification in integer to which it points to it is allowed.  Syntax: int n1=20; int *const ptr=&n1; //Declare constant
Example of constant pointer
Output
Pointer to constant  Pointer to constant object is called as pointer to constant object. Syntax: int n=30; const int *ptr=&n; “const int” means integer constant. We cannot change value of integer. We can change address of such pointer so that it will point to new memory allocation
Example of pointer to constant
Output
Difference Constant pointer Pointer to constant Pointer can be incremented. Pointer is not incremented. Constant pointer is pointing to data object. Pointer is pointing to constant data object.t Declaration :int * const ptr; Declaration: const int *ptr;
THANK YOU

Pointer Basics,Constant Pointers & Pointer to Constant.

  • 1.
    BIRLA INSTITUTE OF TECHNOLOGY TOPIC:- POINTERS BASICS, CONSTANT POINTERS AND POINTERS TO CONSTANT DIVYANSH PARMARTHI MCA/25016/18 SHRADDHA
  • 2.
    POINTERS • A pointeris a variable that points a address of a variable. • A pointer is declared using the * operator before an identifier.
  • 3.
     Declaration ofPointer variables : type *pointer_name; where type is the type of data pointed to For example :- int *numberPointer;
  • 4.
    ADDRESS (&) OPERATOR Pointer address operator is denoted by ‘&’ symbol  The & is a unary operator that returns the memory address of its operand. Example : &n - It gives an address on variable n
  • 5.
    DEREFERENCING OPERATOR (*) We can access to the value stored in the variable pointed to by using the dereferencing operator (*) Syntax: *PointerVariable Example: int V = 101; int *P = &V;
  • 6.
    POINTERS TO POINTERS A pointer to a pointer is a chain of pointers.  In pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value. Pointer Pointer Variable Address Address Value
  • 7.
    Declaration of pointerto a pointer : int **ptr; For example : // local declaration int a; int *p; int **q; // Statment a=58; p=&a; q=&p;
  • 8.
    cout << a<< “ “ <<endl; cout << *p << “ “ <<endl; cout<< **q << “ “ <<endl; OUTPUT 58 58 58
  • 9.
    POINTERS AS PARAMETERS To make changes to a variable that exist after a function ends, we pass the address of the variable to the function For example : #include<iostream.h> void Indirectswap(char *ptr1, char *ptr2) { char c = *ptr1; *ptr1 = *ptr2; *ptr2 = c; }
  • 10.
    int main() { char a= ‘y’; char b = ‘n’; Indirectswap(&a,&b); cout<< a << “and”<< b << endl; return 0; } OUTPUT n and y
  • 11.
    CONSTANT POINTERS AND POINTERSTO CONSTANT BY :- SHRADDHA MALVIYA
  • 12.
    Constant pointer  Constantpointer cannot be modified.  Modification in integer to which it points to it is allowed.  Syntax: int n1=20; int *const ptr=&n1; //Declare constant
  • 13.
  • 14.
  • 15.
    Pointer to constant Pointer to constant object is called as pointer to constant object. Syntax: int n=30; const int *ptr=&n; “const int” means integer constant. We cannot change value of integer. We can change address of such pointer so that it will point to new memory allocation
  • 16.
    Example of pointerto constant
  • 17.
  • 18.
    Difference Constant pointer Pointerto constant Pointer can be incremented. Pointer is not incremented. Constant pointer is pointing to data object. Pointer is pointing to constant data object.t Declaration :int * const ptr; Declaration: const int *ptr;
  • 19.