1. Pointer
• Pointer is a variable, that stores the address of another
variable.
• Syntax:
data_type *pointer_name;
• Here, the data type specifies the type of data to which the
pointer points.
• The pointer name specifies the name of the pointer. It must
preceded with an * (Asterisk).
Examples:
• int *iptr; // iptr is pointer to an integer variable
• float *fptr; // fptr is pointer to an float variable
• char *cptr; // cptr is a pointer to a character variable
2. Initialization of a pointer variable
• The process of assigning the address of another variable to a
pointer variable is known as initialization.
• A pointer can be assigned or initialized with the address of an
variable.
• A pointer variable cannot hold a non-address value.
• A pointer can be assigned or initialized with another pointer
of the same type.
• A pointer can be assigned or initialized with another pointer
of the same type. However, it is not possible to assign a
pointer of one type to a pointer of another type without
explicit type casting.
Example:
P=&n;
3. Initialization of a pointer variable
Example:
• P=&n;
• Where, ‘p’ contains the address of variable ‘n’. The data item
represented by ‘n’ can be accessed by ‘*p’.
• Where * is called the indirection operator.
• We can initialize a pointer variable while declaring it, as
given below.
int num=15;
int *iptr=# // Initialization while declaration
• In the above example, variable num is first declared and then
its address is stored in pointer variable iptr.
4. Pointer Operators
* Represents the value
Also called Indirection operator / Dereferencing
operator / Value of operator.
& It represents the address.
Also called as, direction operator / referencing
operator / Address of operator.
8. Void Pointer or Generic Pointer
• A void pointer is a generic pointer. (i.e) Means it has no
associated data type.
• A void pointer that can point to any data type and is known as
void pointer.
• A void pointer can hold address of any type and can be
“typecasted” to access the values.
• Syntax:
void *pointer_name; /* pointer to void*/
Example:
int i=5;
void *vptr;
vptr=&i;
printf(“Value of vptr =%d”, *(int*)vptr);
9. NULL Pointer
• A Null pointer is a special pointer that does not point to any
memory location.
• It represents an invalid memory location.
• It does not hold the address of any variable.
• It has numeric value 0.
• When a NULL value is assigned to a pointer the pointer is
considered as NULL pointer.
Declaration:
int *nptr=NULL;
or
int *nptr=0;
10. Pointers to Pointers
• In C, a pointer stores the address of another variable which in
turn can store address of another variable and so on.
• Therefore we can have a pointer that stores another pointer’s
address.
• Example:
int val=500, *ptr, **ptr_to_ptr;
ptr=&val;
ptr_to_ptr=&ptr;
Here, *ptr denotes an integer pointer.
**ptr_to_ptr denotes a pointer to an integer pointer.
This is known as multiplication indirections.
11. Pointers to Pointers (An Example)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=50;
int *b;
int **c;
clrscr();
b=&a;
c=&b;
printf("n Value of a is %d ", a);
printf("n Value of a is %d", *(&a));
printf("n Value of a is %d", *b);
printf("n Value of a is %d", **c);
printf("n Value of b and
address of a= %u", b);
printf("n Address of a is %u", &a);
printf("n Address of b is %u", &b);
printf("n Address of a is %u", *c);
printf("n Address of b is %u", &b);
printf("n Address of b is %u", c);
printf("n Address of c is %u", &c);
getch();
}
Output:
Value of a is 50
Value of a is 50
Value of a is 50
Value of a is 50
Value of b and address of a= 65524
Address of a is 65524
Address of b is 65522
Address of a is 65524
Address of b is 65522
Address of b is 65522
Address of c is 65520
12. Relationship between Arrays and
Pointers
• The following relationships exist between arrays and pointers:
1. The name of an array refers to the address of the first element of the
array, i.e. an expression of array type decomposes to pointer type.
A program to depict the relationship between arrays and pointers
#include<stdio.h>
main()
{
int arr[3]={40,15,20};
printf("First element of array is at %p
n",arr);
printf("Second element of array is at %p
n",arr+1);
printf("Third element of array is at %p
n",arr+2);
}
OUTPUT:
First element of array is at 24D7;2242
Second element of array is at 24D7:2244
Third element of array is at 241J7:2248
13. Relationship between Arrays and
Pointers
• The name of an array refers to the address of the first element
of the array but there are two exceptions to this rule:
• a. When an array name is operand of sized operator it does
not decompose to the address of its first element.
14. Relationship between Arrays and Pointers
• In C language, any operation that involves array subscripting is done
by using pointers. The expression of form E1[E2] is automatically
converted into an equivalent expression of form *(E1+E2).
15. Array of Pointers
• Array of pointers is a collection of addresses.
• The addresses in an array of pointers could be the addresses
of isolated variables or the addresses of array elements or any
other addresses.
• Example:
int *p[3];
16. Pointers to an Array
• It is possible to create a pointer that points to a complete array
instead of pointing to the individual elements of an array.
Such a pointer is known as pointer to an array
• Example:
int (*p1)[3];
int (*p2)[2][2];