Pointer
A pointer is a variable that stores memory address or that contains the address of another variable where addresses are the location number always contains the whole number. So, the pointer contains always the whole number. It is called a pointer because it points to a particular location in memory by storing the address of that location.
Syntax-
Data type *pointer name;
Here * before pointer indicate the compiler that variable declared as a pointer.
e.g.
int *p1; //pointer to integer type
float *p2; //pointer to float type
char *p3; //pointer to character type
Initialization of Pointer variable
The pointer variable in C can be divided into three steps.
- Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. While declaring a pointer, we use, the asterisk symbol ( * ) dereference operator before its variable name.
syntax of pointer
data_type *var_name;
Example:-
int *pointer_Name;
2. Pointer initialization
The process of assigning the address of a variable to a pointer variable. The pointer variable contains the address of a variable of the same data type. We generally use the ( & ) address of the operator to get the memory address of a variable and then store it in the pointer variable.
Example 1:-
#include<stdio.h>
void main()
{
int x = 10;
int *p = &x;
printf(" x = %d p = %d",x, p);
}
Example 2:-
int var = 10;
int * ptr;
ptr = &var;
3. Pointer Dereferencing
The process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
Once a pointer has been assigned the address of a variable. To access the value of a variable, the pointer is dereferenced, using the indirection operator *.
int a,*p;
a = 10;
p = &a;
printf("%d",*p); //this will print the value of a.
printf("%d",*&a); //this will also print the value of a.
printf("%u",&a); //this will print the address of a.
printf("%u",p); //this will also print the address of a.
printf("%u",&p); //this will also print the address of p.
Types of Pointer in C
Pointers in C can be classified into many different types based on the parameter on which we are defining their types.
- Null Pointer
A pointer that is not assigned any value but NULL is known as a NULL pointer. If you don’t have any address to be specified in the pointer at the time of declaration, you can assign a NULL value.
Syntax
data_type *pointer_name = NULL;
or
pointer_name = NULL
Example:-
#include <stdio.h>
int main(){
int *ptr = NULL; //null pointer
printf("The value inside variable ptr is:
%d",ptr);
return 0;
}
Output
The value inside variable ptr is : 0
2. Void Pointer
It is a pointer that has no associated data type with it. They are also called generic pointers. A void pointer can hold addresses of any type and can be typecast to any type.
Syntax:-
void * pointer_name;
Example:-
#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:-
The size of pointer is : 8
3. Wild Pointer
The Wild pointers are those pointers that have been called uninitialized pointers in C which point to arbitrary (random) memory locations. This wild pointer may lead a program to behave wrongly or to crash.
This type of C pointer is not efficient. Because they may point to some unknown memory location which may cause problems in our program.
Syntax
datatype * pointer_name;
Example:-
#include <stdio.h>
int main() {
int *ptr; // Declare a pointer
*ptr = 5; // Assign a value to a pointer
printf("The value of the pointer is: %d\n", *ptr);
return 0;
}
4. Dangling Pointer
Dangling pointers are pointers that point to a memory location that has been deleted (or freed).
Example:- //*Deallocation of Memory*//
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
ptr = NULL;
}
4. Integer Pointer
An integer pointer in C is a pointer variable that is specifically designed to store the memory address of an integer variable.
Syntax:-
int *ptr; // Declaration of an integer pointer
Example:-
#include <stdio.h>
int main() {
int num = 42;
// Declaration of an integer pointer and assigning the address of 'num' to it
int *ptr = #
// Accessing the value using the integer pointer
printf("Value of num: %d\n", *ptr);
// Modifying the value using the integer pointer
*ptr = 99;
// Displaying the modified value
printf("Modified value of num: %d\n", num);
return 0;
}
5. Array Pointer
An array pointer is a pointer that stores the memory address of the first element of an array.
Syntax:-
int *ptr = &arr_name;
Example:-
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++)
{
printf("%d", *p);
p++;
}
6. Function Pointer
A function pointer is a pointer that stores the memory address of a function.They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code.
Syntax:-
void (*func_ptr)(int) = &function_name;
Example:-
int add(int a, int b) {
return a + b;
}
int (*ptr)(int, int) = add;
7. Double Pointer(Pointer to Pointer):-
A double pointer is a pointer that stores the memory address of another pointer.This type of pointer is known as Pointer-to-Pointer.
Syntax:-
datatype ** pointer_name;
Example:-
int num = 42;
int *ptr1 = #
int **ptr2 = &ptr1;
8. Character Pointer
A character pointer is a pointer that stores the memory address of a character.
Syntax
char *ptr;
Example:-
char ch = 'A';
char *ptr = &ch;
Advantages of pointers in C
- Pointers are useful for accessing memory locations.
- An Array or a structure can be accessed efficiently with pointers
- Pointers are used for dynamic memory allocation and deallocation.
- Pointers are used to form complex data structures such as linked list, graph, tree, etc.
- Pointers reduce the length of the program and its execution time as well.
Disadvantages of Pointer in C
- Pointers are also responsible for memory leakage.
- Programmers find it very difficult to work with the pointers; therefore it is programmer’s responsibility to manipulate a pointer carefully.
- Pointers are comparatively slower than variables
- Memory corruption can occur if an incorrect value is provided to pointers.
- Pointers can lead to various errors such as segmentation faults or can access a memory location which is not required at all.
Arithmetic Operator in Pointer ‘C’
Arithmetic operators in C are used to perform mathematical operations on operands. There are a total of 9 arithmetic operators in C, which can be divided into two categories: binary and unary.
Binary arithmetic operators operate on two operands, while unary arithmetic operators operate on a single operand.
The binary arithmetic operators in C:
- Addition (
+
): Adds the values of two operands. - Subtraction (
-
): Subtracts the value of the second operand from the first operand. - Multiplication (
*
): Multiplies the values of two operands. - Division (
/
): Divides the value of the first operand by the second operand. - Modulus (
%
): Returns the remainder of the division of the first operand by the second operand.
The unary arithmetic operators in C:
- Increment (
++
): Increments the value of the operand by 1. - Decrement (
--
): Decrements the value of the operand by 1.
Example:- //*Working Of arithmetic operators*//
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Example:-
// Working of increment and decrement operators//
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000