What are the Uses of Pointers in C?
In C, the pointer is a variable used for storing the address of a variable. The variable can be of any type such as int, char, array, etc. The pointer size depends on the computer architecture, however, for a 32-bit system, the pointer used is 2 bytes.
Following are the usage of pointers in C:
- For passing the argument by using references.
- For accessing the elements of an array.
- For dynamic memory allocation by using malloc() and calloc() functions .
- Used in arrays, functions to improve the performance of code.
- For returning multiple values.
- Implementing a data structure.
- For doing system-level programming.
What are the Features of Pointers in C?
Following are the features of using pointers in C:
- It saves the space of memory.
- The execution time of code is faster when using a pointer since the data are manipulated by using an address, which is also used for direct access to the memory location.
- By using pointers, memory can be accessed efficiently because a pointer is used for assigning as well as releasing memory. Hence it can be concluded that the memory of the pointers is located dynamically
- It can be used with a data structure for representing two or multi-dimensional arrays.
- By using a pointer, an array of any type can be accessed.
- Has a feature for file handling.
- Can allocate memory dynamically.
- If any pointer is declared in the base class, it could access the object of the derived class, however, the opposite of that is not possible.
Examples of the Uses of Pointers in C
Example 1:
Program of using a pointer for printing the address as well as the value of a number.
Output:
/tmp/FcdQjRVXxM.o
The address of n is 2807cc
The value of n is 13
Explanation:
The variable n stores the address of the number in a variable. The variable p contains the address of the number. Here * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
Example 2:
C program of assigning a pointer to another variable.
Output:
Explanation:
Here, 3 is assigned to the variable b. And, the address of b is assigned to the pointer.
Example 3:
C program for changing the value pointed by other pointers.
Output:
Explanation:
In this, first, we have assigned the address of b to the pointer. Then, we changed the value of b to 1. Since a and the address of b are the same, *a gives us 1.
Learn More about Pointers in C
Conclusion
- In C, the pointer is a variable used for storing the address of a variable. The variable can be of any type such as int, char, array, etc. The pointer size depends on the computer architecture, however, for a 32-bit system, the pointer used is 2 bytes.
- Following are the usage of pointers in C:
- For passing the argument by using references.
- For accessing the elements of an array.
- For dynamic memory allocation by using malloc() and calloc() functions .
- Used in arrays, and functions to improve the performance of code.
- For returning multiple values.
- Implementing a data structure.
- For doing system-level programming.
- For accessing the elements of an array.
- For dynamic memory allocation by using malloc() and calloc() functions .
- Used in arrays, functions to improve the performance of code.
- For returning multiple values.
- Implementing a data structure.
- For doing system-level programming.