You are on page 1of 10

What are Pointers?

Pointers in C language is a variable that stores/points the address of another variable. A Pointer in
C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging
to any of the data type such as int, float, char, double, short etc.

Pointer Syntax : data_type *var_name; 


Example : int *p;  char *p;
Where, * is used to denote that “p” is pointer variable and not a normal variable.

KEY POINTS TO REMEMBER ABOUT POINTERS IN C:


 Normal variable stores the value whereas pointer variable stores the address of the variable.
 The content of the C pointer always be a whole number i.e. address.
 Always C pointer is initialized to null, i.e. int *p = null.
 The value of null pointer is 0.
 & symbol is used to get the address of the variable.
 * symbol is used to get the value of the variable that the pointer is pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
 Two pointers can be subtracted to know how many elements are available between these two
pointers.
EXAMPLE PROGRAM FOR POINTERS IN C:

#include <stdio.h>
int main()
{
   int *ptr, q;
   q = 50;
/* address of q is assigned to ptr */
   ptr = &q;
   /* display q's value using ptr variable */
   printf("%d", *ptr);
   return 0;
}

Output:
50

Benefits(use) of pointers in c:

 Pointers provide direct access to memory


 Pointers provide a way to return more than one value to the
functions
 Reduces the storage space and complexity of the program
 Reduces the execution time of the program
 Provides an alternate way to access array elements
 Pointers can be used to pass information back and forth between
the calling function and called function.
 Pointers allows us to perform dynamic memory allocation and
deallocation.
 Pointers helps us to build complex data structures like linked list,
stack, queues, trees, graphs etc
 Pointers allows us to resize the dynamically allocated memory
block.
 Addresses of objects can be extracted using pointers

Pointer Arithmatic:
There are 4 Arithmetic Operation that can used on Pointer.++,--,+,-
1.int *i
i++;
2.int *I;
i--;

3.int *p1;
int *p2;
*p1+*p2;
4.int *p1;
int *p2;
*p1-*p2;

Program for pointer arithmetic(32-bit machine)


#include <stdio.h>

int main()
{

int m = 5, n = 10, o = 0;

int *p1;

int *p2;

int *p3;

p1 = &m; //printing the address of m

p2 = &n; //printing the address of n

printf("p1 = %d\n", p1);

printf("p2 = %d\n", p2);

o = *p1+*p2;

printf("*p1+*p2 = %d\n", o);//point 1

p3 = p1-p2;

printf("p1 - p2 = %d\n", p3); //point 2

p1++;
printf("p1++ = %d\n", p1); //point 3

p2--;

printf("p2-- = %d\n", p2); //point 4

//Below line will give ERROR

printf("p1+p2 = %d\n", p1+p2); //point 5

return 0;

Output:
p1 = 2680016

p2 = 2680012

*p1+*p2 = 15

p1-p2 = 1

p1++ = 2680020

p2-- = 2680008
Array of pointers:

#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};

int i, *ptr[MAX];

for ( i = 0; i < MAX; i++) {

ptr[i] = &var[i]; /* assign the address of integer. */

for ( i = 0; i < MAX; i++) {

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


}

return 0;

Output:

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

Passing Pointer to a Function:


we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address
of the variable is passed instead of the value.

Example:

#include <stdio.h>
void salaryhike(int *var, int b)
{
*var = *var+b;
}
int main()
{
int salary=0, bonus=0;
printf("Enter the employee current salary:");
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
return 0;
}
Output:

Enter the employee current salary:10000


Enter bonus:2000
Final salary: 12000

Pointers as Function Argument:

#include <stdio.h>

void swap(int *a, int *b);

int main()
{
int m = 10, n = 20;
printf("m = %d\n", m);
printf("n = %d\n\n", n);

swap(&m, &n); //passing address of m and n to the swap function


printf("After Swapping:\n\n");
printf("m = %d\n", m);
printf("n = %d", n);
return 0;
}

/*
pointer 'a' and 'b' holds and
points to the address of 'm' and 'n'
*/
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

Output:

m = 10
n = 20
After Swapping:
m = 20
n = 10

You might also like