- Pointers in C are easy and fun to learn. Some C programming 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. Let’s start learning them in simple and easy steps.
- C Pointer is a variable that stores/points the address of another variable. C Pointer 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.
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.
What Are Pointers?
Different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to “point to variables”. Lets try to understand the concept.
About Above Diagram
- A normal variable ‘var’ has a memory address of 1001 and holds a value 50.
- A pointer variable has its own address 2047 but stores 1001, which is the address of the variable ‘var’
Keypoints to Remember
- 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 pointer 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.
- But, Pointer addition, multiplication, division are not allowed.
- The size of any pointer is 2 byte (for 16 bit compiler).
Advantages Of Pointers
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 function using pointer.
3) It makes you able to access any memory location in the computer’s memory.
Usage Of Pointers
There are many usage of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where 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.
Declaring a Pointer variable
General syntax of pointer declaration is,
Data type of pointer must be same as the variable, which the pointer is pointing. void type pointer works with all data types, but isn’t used oftenly.
Initialization of Pointer Variable
Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In C language address operator
& is used to determine the address of a variable. The
& (immediately preceding a variable name) returns the address of the variable associated with it.