The Pointer in C, is a variable that stores address of another variable. A pointer variable can also be used to refer to another pointer function. A pointer variable can be incremented or decremented, i.e., to point to the next or previous memory location. The purpose of pointer variable is to save memory space and achieve faster execution time and some tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer.
As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.
Consider the following example, which prints the address of the variables
#include <stdio.h>
int main () {
int v1;
char v2[10];
printf("Address of v1 variable: %x\n", &v1 );
printf("Address of v2 variable: %x\n", &v2 );
return 0;
}
When the above code is compiled and executed, it produces Address of variable.
Like any variable or constant, you must declare a pointer variable before using it to store any variable address.
The general form of a pointer variable declaration is −
datatype *var_name;
Pointer variables are special variables that are used to store addresses rather than values.
Here is how we can declare pointers.
int* p;
Here, we have declared a pointer p of int type.
You can also declare pointers in these ways.
int *p1;
int * p2;
Let's take another example of declaring pointers.
int* p1, p2;
Here, we have declared a pointer p1 and a normal variable p2.
Assigning addresses to Pointers Variable
Let's take an example.
int *pc, c;
c = 15;
pc = &c;
Here, 15 is assigned to the c variable. And, the address of c is assigned to the pc pointer.
Get Value of Thing Pointed by Pointers
To get the value of the thing pointed by the pointers, we use the * operator.
For example:
int *pc, c;
c = 15;
pc = &c;
printf("%d", *pc); // Output: 15
Here, the address of c is assigned to the pc pointer variable. To get the value stored in that address, we used *pc.
Note: In the above example, pc is a pointer, not *pc. You cannot and should not do something like *pc = &c;
By the way, * is called the dereference operator (when working with pointers). It operates on a pointer and gives the value stored in that pointer.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
4)Pointers provide an efficient way for accessing the elements of an array structure.
5)Pointers are used for dynamic memory allocation as well as deallocation.
6)Pointers are used to form complex data structures such as linked list, graph, tree, etc.
Disadvantages of Pointers in C
- Pointers are a little complex to understand.
- Pointers can lead to various errors such as segmentation faults or can access a memory location which is not required at all.
- If an incorrect value is provided to a pointer, it may cause memory corruption.
- Pointers are also responsible for memory leakage.
- Pointers are comparatively slower than that of the variables.
- Programmers find it very difficult to work with the pointers; therefore it is programmer's responsibility to manipulate a pointer carefully.
Usage of pointer
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.
Types of Pointers in C
Following are the different Types of Pointers in C:
- Null Pointer
We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Output:
The value inside variable p is:
0
- Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.
Following program illustrates the use of a void pointer:
#include <stdio.h>
int main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}
Output:
The size of pointer is:4
3.Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of C pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
int main()
{
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}
Other types of pointers in 'c' are as follows:
- Dangling pointer
- Complex pointer
- Near pointer
- Far pointer
- Huge pointer
Direct and Indirect Access Pointers
In C, there are two equivalent ways to access and manipulate a variable content
- Direct access: we use directly the variable name
- Indirect access: we use a pointer to the variable
Let's understand this with the help of program below
#include <stdio.h>
/* Declare and initialize an int variable */
int var = 1;
/* Declare a pointer to int */
int *ptr;
int main( void )
{
/* Initialize ptr to point to var */
ptr = &var;
/* Access var directly and indirectly */
printf("\nDirect access, var = %d", var);
printf("\nIndirect access, var = %d", *ptr);
/* Display the address of var two ways */
printf("\n\nThe address of var = %d", &var);
printf("\nThe address of var = %d\n", ptr);
/*change the content of var through the pointer*/
*ptr=48;
printf("\nIndirect access, var = %d", *ptr);
return 0;}
After compiling the program without any errors, the result is:
Direct access, var = 1
Indirect access, var = 1
The address of var = 42024
The address of var = 42025
Indirect access, var = 48
C Pointers & Arrays with Examples
Traditionally, we access the array elements using its index, but this method can be eliminated by using pointers. Pointers make it easy to access each array element.
#include <stdio.h>
int main()
{
int a[5]={1,2,3,4,5}; //array initialization
int *p; //pointer declaration
/*the ptr points to the first element of the array*/
p=a; /*We can also type simply ptr==&a[0] */
printf("Printing the array elements using pointer\n");
for(int i=0;i<5;i++)
//loop for traversing array elements
{
printf(“\t%x”,*p);
//printing array elements
p++;
//incrementing to the next element, you can also write p=p+1
}
return 0;
}
Output
1 2 3 4 5
C Pointers and Strings with Examples
A string is an array of char objects, ending with a null character '\ 0'. We can manipulate strings using pointers. This pointer in C example explains this section
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="Hello Csdt”;
char *p;
p=str;
printf("First character is:%c\n",*p);
p =p+1;
printf("Next character is:%c\n",*p);
printf("Printing all the characters in a string\n");
p=str; //reset the pointer
for(int i=0;i<strlen(str);i++)
{
printf(“%c\t”,*p);
p++;
}
return 0;
}
Output
First character is:H
Next character is:e
Printing all the characters in a string
H e l l o
C s d t!