Learning about pointers is easy and fun in C. Some C programming tasks are performed more easily with pointers. But dynamic memory allocation cannot be performed without using pointers. Pointer is a derived datatype and is considered as a system programming primitive.
What is Pointer?
Pointer is a variable that is basically used to store the address. The capability of that pointer is to fetch the content whose address stored in a pointer variable.
Pointer can be int*p , char*p, float*p, double*p. So,
char ch= ‘A’;
char *p= &ch;
double d= 9.6;
double *p=&d;
Operators in Pointers:-
- &(Address-of operator): & is a unary operator used to store address of any type of variable.
- *(Content-of operator): * is a unary operator which is used to fetch contents whose address is stored in a pointer variable.
Note- Here, unary operator means only 1 operand.
The size of pointer depends on the architecture. However, in 32-bit architecture, the size of pointer is 2 bytes.
As it can be seen from above, address of fff4 is stored by pointer variable.
50 is value of number variable. But aaa3 is address of pointer variable.
So basically,
Variable is a value stored in named storage or memory address.
Pointer is a variable that points to that storage or memory address of another variable.
Declaring a Pointer:-
We can declare a pointer in the following way,
data_type * pointer_variable_name;
- ‘data_type’ indicates the type of variable to which the pointer is pointing.
- * (asterisk) is basically an indirection operator that declares pointer.
#include <stdio.h>
int main() {
int b = 10; // Declaration of variable
int *q; // Declaration of pointer variable
q = &b; // Pointer q stores the address of variable b
printf("Variable q stores address as: %x\n", q); // Address is accessed
printf("Variable q stores value as: %d\n", *q); // Value is accessed
return 0;
}
/*
Output:
Variable q stores address as: 60ff08
Variable q stores value as: 10
*/
Differentiating between * and &:-
#include <stdio.h>
int main() {
int *ab, b;
b = 25;
printf("Address of b: %p\n", &b);
printf("Value of b: %d\n\n", b); // 25
ab = &b;
printf("Address of pointer ab: %p\n", ab);
printf("Content of pointer ab: %d\n\n", *ab); // 25
b = 14;
printf("Address of pointer ab: %p\n", ab);
printf("Content of pointer ab: %d\n\n", *ab); // 14
*ab = 5;
printf("Address of b: %p\n", &b);
printf("Value of b: %d\n\n", b); // 5
return 0;
}
/*
Output:
Address of b: 2686784
Value of b: 25
Address of pointer ab: 2686784
Content of pointer ab: 25
Address of pointer ab: 2686784
Content of pointer ab: 14
Address of b: 2686784
Value of b: 5
*/
- int *ab, b;
Here, ab is a pointer and b is a normal variable (both are type of int) As they both are not initialised initially, so pointer ab points to either no address or a random address. While, b has an address but contains random garbage value.
2. b= 25;
25 is assigned to variable b. That is, 25 is stored in memory location of variable b.
3. ab = &b;
pointer ab is assigned with the address of variable b.
4. b = 14;
This assigns 14 to variable b.
5. *ab= 5;
This changes the value at the memory location pointed by the pointer ab to 5.
Types Of Pointers:-
- Dangling Pointer
- Far Pointer
- Void Pointer
- Complex Pointer
- Huge Pointer
- Null Pointer
- Wild Pointer
- Near Pointer
Advantages Of Pointers:-
- Memory locations, elements of an array structure, etc. can be accessed through pointers.
- They are used to form complex data structure such as linked list, graph, tree, etc.
- Pointers are widely used in dynamic memory allocation and de-allocation.
Disadvantages Of Pointers:-
- Pointers may lead to errors such as segmentation faults and are also responsible for memory leakage.
- If an incorrect value is provided, they may cause memory corruption.
- It is a bit difficult to work with pointers, so it is programmer’s responsibility to manipulate a pointer carefully.
Following important pointer concepts should be clear to any C programmer:-
Pointer program for swapping of 2 numbers without using the 3rd variable-
#include<stdio.h>
int main() {
int p = 40, q = 50, *a1 = &p, *a2 = &q;
printf("Before swap: *a1=%d *a2=%d", *a1, *a2);
*a1 = *a1 + *a2;
*a2 = *a1 - *a2;
*a1 = *a1 - *a2;
printf("\nAfter swap: *a1=%d *a2=%d", *a1, *a2);
return 0;
}
/*
Output:
Before swap: *a1=40 *a2=50
After swap: *a1=50 *a2=40
*/
Summary:-
- A pointer is basically a memory location where data is stored.
- They cab be used with array and string to access elements more efficiently.
- Arithmetic operations can be done on a pointer which is known as pointer arithmetic.
- Pointers also point to function making it easy to call those different functions in the case of defining an array of pointers.
- Also, typecast void pointer are used when you want to deal different variable data type.