2. POINTERS
• A pointer is a variable that points a address of a variable.
• A pointer is declared using the * operator before an identifier.
3. Declaration of Pointer 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 pointer to a pointer :
int **ptr;
For example :
// local declaration
int a;
int *p;
int **q;
// Statment
a=58;
p=&a;
q=&p;
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
12. 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
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
18. 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;