WHAT IS POINTER
DECLARATION OF POINTER
HOW TO USE A POINTER ?
WHAT IS NULL POINTER?
POINTER AS A FUNCTION ARGUMENT
POINTER TO ARRAY
ARITHEMETICS OF POINTERS
POINTER TO STRUCTURE
INDEX
The pointer in C language is a variable which
stores the address of another variable.
This variable can be of type int, char, array,
function, or any other pointer.
The size of the pointer depends on the
architecture.
WHAT IS POINTER
ptr i
POINTER
1
The pointer in c language can be declared using * (asterisk
symbol). It is also known as indirection pointer used to
dereference a pointer.
The general form of a pointer variable declaration is :
dataType*ptr_name;
Example:
int *p1 //Pointer to an integer variable
double *p2 //Pointer to a variable of data type double
char *p3 //Pointer to a character variable
float *p4 //pointer to a float variable
DECLARATION OF POINTERS
POINTER
2
There are few important operations , which we will do with the help of
pointers very frequently .
Dereferencing a pointer means getting the value stored in the
memory at the address which the pointer "points" to.
Dereferencing is done by using unary operator * that returns the
value of the variable located at the address specified by its operand.
1. we define a pointer variable
2. assign the address of a variable to the pointer
3. finally access the value at the address available in the pointer
variable by dereferencing
How To Use Pointers?
POINTER
3
The * is the value-at-address operator, also called the indirection
operator or dereferencing. It is used both when declaring a pointer
and when dereferencing a pointer.
the & is the address-of operator and is used to reference the memory
address of a variable.
By using the & operator in front of a variable name we can retrieve the
memory address-of that variable. It is best to read this operator as
address-of operator
Following code shows some common notations for the value-at-
address (*) and address-of (&) operators
POINTER
4
5
CODE-1
3
i
j
1000
1000
1004
6
CODE-2
POINTER
7
NULL Pointer
What is a Null Pointer?
A Null Pointer is a pointer that does not point to any
memory location. It stores the base address of the
segment. The null pointer basically stores the Null
value.
<data type> *<ptr name> = NULL;
Syntax
8
CODE-3
POINTER
Pointer as Function Argument
Declaration
(type) (*pointer_name)(parameter);
Pointer as a function parameter is used to hold addresses of
arguments passed during function call. This is also known as call by
reference. When a function is called by reference any change made
to the reference variable will effect the original variable.
9
POINTER
10
CODE-4
An array of pointers allows you to numerically
index a large set of variables using its address.
Declaration of an array of pointers to an integer −
int *ptr[MAX];
Pointer to Array
POINTER
11
12
CODE-5
Increment (++)
Decrement (--)
Addition (+)
Subtraction (-)
C pointer is an address, which is a numeric value. Therefore, you can perform
arithmetic operations on a pointer just as you can a numeric value.
There are four arithmetic operators that can be used on pointers:
ARITHEMETICS OF POINTERS
POINTER
13
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points
to the address 1000. Assuming 64 bit integers, let us perform the following arithmetic
operation on the pointer:
Now, after the above operation, the ptr will point to the location 1004 because each time ptr
is incremented, it will point to the next integer location which is 4 bytes next to the current
location.
++ptr
POINTER
14
15
CODE-6
Declaration:
Following is the declaration for pointers to structures in C programming −
struct name *ptr;
Accessing:
To access the pointers to structures.
Ptr-> membername;
The members of the structure can be accessed using a special operator
called as an arrow operator ( -> )
POINTER TO STRUCTURE
POINTER
16