In this article Pointers in C we give the information about pointer is a special derived data type that holds the address of another variable.
Pointers in C:
- A pointer in the C programming language is a variable that is used to store the address of another variable. Basically, a Pointer is essentially a simple Integer variable that holds a memory address that points to a value rather than holding the actual value.
- Pointers are special type of variables which are used to store address instead of value.
- Pointer is a special derived data type that holds the address of another variable.
- Pointers variables can be of different data types like – (int, char, float, double, array, function) which are very easy to declare.
- Its size depends on the architecture of the computer, like for 32 bit architecture it takes 2 bytes. Pointer in C language is denoted by asterisk ( * ) symbol.
Here ptr is a pointer, which is of integer type. It holds the address of variable ‘n’. Through this, both the address and value of variable ‘n’ can be printed.
Declaring a pointer:
Like all other data types, pointer also needs to be declared.
Where the data type can be int, float, or char, and the pointer name is a name given by the user, the asterisk (*) symbol indicates that the variable is a pointer.
Pointers in C:
Types of Pointer in C:
1. NULL Pointer:
The pointer which is not assigned any value is called NULL pointer. The NULL pointer in C programming does not point to the address of any variable. When printed it can print garbage value, so it is always assigned NULL value.
2. Void pointer:
Void pointer in C is a pointer that is not associated with any data type. It points to some data location in storage. When a pointer is declared with a void keyword it is called a void pointer.
3. Wild pointer:
A wild pointer is merely declared but not assigned the address of any variable. These are different from pointers i.e. they also store a memory address but point to unallocated memory or a data value that has been deleted. Such pointers are known as Wild Pointers.
4. Dangling pointer:
Pointers pointing to deallocated memory blocks are known as dangling pointers. This situation produces an error known as the dangling pointer problem.
Usage of Pointer in C:
- Pointer is used in dynamic memory allocation and deallocation.
- It is used in complex data structures like linked list, graph, tree etc.
- Pointers are used in system level programming where memory addresses are very important.
- To implement the data structure.
- Pointers are used when multiple values have to be returned at once.
- Pointers can also be used to access array elements.
- When we have to pass the address of a variable as a reference to the function, then pointer is used.
- Pointer is used in file handling.
Advantages of pointer:
- They are used in dynamic memory allocation. Dynamic memory allocation is not possible without pointer.
- They are also used in Array, Function and Structure.
- It reduces the program code, that is, reduces the size of the code.
- Through this, the memory address of a variable can be directly accessed.
- Reduces the execution time of the program.
- In Call by reference method, pointer is used to pass the value between two functions.
Disadvantages of pointer:
- It is slow as compared to other variables.
- If no value is assigned to Pointer, it returns garbage value, so it has to do with NULL Pointer.
Example of the pointer in C programming –
n = 100;
ptr = &n;
printf(“Value of N = %d, Assigned address of N to ptr = %d”,n, ptr);
Value of a = 100, Assigned address of a to ptr = 2000