You are on page 1of 30

Pointers

A pointer is a variable that stores the memory address of another variable as its value.
A pointer variable points to a data type (like int ) of the same type, and is created with the *
operator.
Pointers are special variables that store the memory address, instead of value like in
usual variables. Pointers always hold addresses as a whole number. Pointers in C are
always initialized to NULL.
Pointers are used to store and manage the addresses of dynamically allocated blocks
of memory. Such blocks are used to store data objects or arrays of objects. Most structured
and object-oriented languages provide an area of memory, called the heap or free store,
from which objects are dynamically allocated.

Declaration:
The * symbol indicates that the variable is a pointer. To declare a variable as a pointer, you
must prefix it with *. In the example above, we have done a pointer declaration and named
ptr1 with the data type integer.

Syntax:
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer.

Example:
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointe

Initialization:

Pointer Initialization is the process of assigning the address of a variable to a


pointer. In C language, the address operator & is used to determine the address of a
variable. The & (immediately preceding a variable name) returns the address of the
variable associated with it.

Syntax:
data_type*pointer_variable=&variable;
data_type*pointer_variable;
pointer_variable=&variable;

Example:

#include<stdio.h>

int main(void)

int a = 10;

// declare a pointer

int *ptr;

// assign value to pointer

ptr = &a;

printf("Value at ptr is: %d \n", *ptr);

printf("Address pointed by ptr is: %p \n",


ptr);

return 0;
}

Types of pointers:
There are eight different types of pointers which are as follows −
 Null pointer
 Void pointer
 Wild pointer
 Dangling pointer
 Complex pointer
 Near pointer
 Far pointer
 Huge pointer

Null Pointer
You create a null pointer by assigning the null value at the time of pointer declaration.
This method is useful when you do not assign any address to the pointer. A null pointer
always contains value 0.

Example
Following is the C program for the null pointer −

#include <stdio.h>
int main(){
int *ptr = NULL; //null pointer
printf("The value inside variable ptr is:
%d",ptr);
return 0;
}

Void Pointer
It is a pointer that has no associated data type with it. A void pointer can hold addresses of
any type and can be typecast to any type.
It is also called a generic pointer and does not have any standard data type.
It is created by using the keyword void.

Example
Following is the C program for the void pointer −
#include <stdio.h>
int main(){
void *p = NULL; //void pointer
printf("The size of pointer is:%d
",sizeof(p)); //size of p depends on compiler
return 0;
}

Output
When the above program is executed, it produces the following result –
The size of pointer is:8

Wild Pointer
Wild pointers are also called uninitialized pointers. Because they point to some arbitrary
memory location and may cause a program to crash or behave badly.
This type of C pointer is not efficient. Because they may point to some unknown memory
location which may cause problems in our program. This may lead to the crashing of the
program.
It is advised to be cautious while working with wild pointers.

Example
Following is the C program for the wild pointer −
#include <stdio.h>
int main(){
int *p; //wild pointer
printf("
%d",*p);
return 0;
}
Process returned -1073741819 (0xC0000005) execution time : 1.206 s
Press any key to continue
the Use Cases of Pointers
1. Pointer arithmetic.
2. Pointer to pointer.
3. Array of pointers.
4. Call by value.
5. Call by reference.
Pointer arithmetic:

 Increment: You can use this operator to jump from one index to the next index in an array.

Syntax:

ptr++;

Example:

#include <stdio.h>

int main() {

int arr[3] = {50, 150, 200};

int *ptr;

ptr = arr;

for (int i = 0; i < 3; i++)

printf(“Value of *ptr = %d\n”,*ptr);

printf(“Address of *ptr = %d\n”,ptr);

ptr++;
}

 Decrement: You can use this operator to jump from one index to the previous index in an
array.

Syntax:

Ptr--;

Example:

#include<stdio.h>

int main()

int arr[3]={50, 150, 200};

int *ptr;
ptr = &arr[2];

for (int i=0;i<3;i++)

printf("Value of *ptr = %d\n", *ptr);

printf("Address of *ptr = %d\n\n", ptr);

ptr--;

 Integers added to a Pointer: You can use this operator to jump from one index to the next
ith index in an array.

Syntax:

ptr+=i; // where ‘i’ is an integer


Example:

#include <stdio.h>

int main() {

int arr[5] = {10, 100, 200, 300, 500};

int *ptr;

ptr = &arr[0];

for (int i = 0; i < 5; i++) {

printf("Value of *ptr = %d\n", *ptr);

printf("Address of *ptr = %d\n\n", ptr);

ptr=ptr+2;

}
 Integers Subtracted from a Pointer: You can use this operator to jump from one index to
the previous ith index in an array.

Syntax:

ptr-=i; // where ‘i’ is an integer

Example:

#include <stdio.h>

int main() {

int arr[5] = {10, 100, 200, 300, 500};


int *ptr;

ptr = &arr[4];

for (int i = 0; i<5; i++)

printf("Value of *ptr = %d\n", *ptr);

printf("address of *ptr = %d\n\n", ptr);

ptr-=2;

 Precedence:
 Operators * and & are given the same priorities as unary operators (increment++,
decrement--).

 The unary operators *, &, ++, - are evaluated from right to left in the same expression.

 If a P points to an X variable, then you can interchange X with *P.

Expression Equivalent Expression

Y=X+1 Y=*P+1

X=X+10 *P=*P+10

X+=2 *P+=2

++X ++*P

X++ (*P)++

Pointer to Pointer:
In this situation, a pointer will indirectly point to a variable via another pointer.

Syntax:

Int **ptr;

Example:

#include <stdio.h>

int main ()

int var, *ptr1, **ptr2;

var = 10;

ptr1 = &var;

ptr2 = &ptr1;

printf("Value of var = %d\n", var );

printf("Value available at *ptr1 = %d\n", *ptr1 );

printf("Value available at **ptr2 = %d\n", **ptr2);

return 0;

}
An Array of Pointer:

An array of the pointer is an array whose every element is a pointer.

Syntax:

int *arr[n] //where n is size of array.

Example:

#include <stdio.h>

int main ()

int a[3] = {10, 100, 200},n=3;

int i, *ptr[3];
for ( i = 0; i < 3; i++)

ptr[i] = &a[i];

for ( i = 0; i < n; i++)

printf("Value of a[%d] = %d\n", i, *ptr[i] );

return 0;

Call By Value:

In ‘call by value’, you must copy the variable's values and pass them in the function call as a
parameter. If you modify these parameters, then it doesn't change the value of the actual
variable.
Example:

#include<stdio.h>

void change(int num)

printf("Before adding value inside function num=%d\n",num);

num=num+100;

printf("After adding value inside function num=%d\n",num);

int main()

int x=100;

printf("Before function call x=%d \n",x);

change(x);
printf("After function call x=%d \n",x);

return 0;

Call By Reference:

In call by reference, you must take the variable's address and pass it in the function call as a
parameter. If you modify these parameters, then it will change the value of the actual variable
as well.

Example:

#include<stdio.h>

void change(int *num)

{
printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n",*num);

int main()

int x=100;

printf("Before function call x=%d \n",x);

change(&x);//passing reference in function

printf("After function call x=%d \n",x);

return 0;

Uses of pointers:
 To pass arguments by reference.
 For accessing array elements.
 To return multiple values.
 Dynamic memory allocation.
 To implement data structures.
 To do system-level programming where memory addresses are useful.
:

You might also like