Pointers in C
Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the variable that it is pointing to. A pointer is also used to refer to other pointer functions present. Increment and decrement operations can be done on the pointer variable; that is, it can point to the next or the previous memory location. The vital purpose of the pointer is to save the memory space and get faster execution of the programs and make it more efficient.
Pointers in the C standard are fun and easy to learn and will be a massive threat if not understood and appropriately implemented. Many tasks within the programs, such as dynamic memory allocation, cannot and will not be performed without the intervention of the pointers.
Hence, it is a necessary operation that every programmer and developer learn. Everymostlyinvolves variable is a memory location in any programming language, and every memory location has its address defined in its way.
Like any other constant or a variable, one must always declare a pointer before using it to store the variable’s address. It can be accessed using the operator named ‘ampersand’ (&) that denotes the address in the memory. Pointers are declared using the asterisk symbol (*).
Syntax1
datatype *variable_name 1;
- The above-mentioned syntax is to declare a pointer.
- The declaration must be a valid datatype present in the C standard
- The variable_name can be anything that represents the name of the pointer variable.
- The ‘asterisk’ (*) is used to declare the pointer.
E.g.:
int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */
Note:The difference between the pointer of several data types is that the data type of that variable or the pointer’s constant points to.
Syntax2
dataype variable_name1, *variable_name2; variable_name2 = &variable_name1;
The above-mentioned syntax is to assign the address of the variable to a pointer.
E.g.
#include <stdio.h> #include <conio.h> int main () { int var1; char var2[10]; printf("The address of the var1 variable: %x\n", &var1 ); printf("The address of the var2 variable: %x\n", &var2 ); return 0; }
Output
Address of the operator (&)
The symbol (&) returns the address of the variable. But it should be used along with %u in order to display the address of that particular variable.
E.g.
#include <stdio.h> #include <conio.h> int main() { int n = 10; printf (“The value of the number ‘n’ is %d \n and the address of the number ‘n’ is %u”, n, &n); return 0; }
Output
Steps on how to use pointers
- Define the pointer variable in the beginning of the program.
- Assigns the address of that variable that was defined earlier to a pointer.
- Access the value of that address that was assigned and available in the pointer variable.
- It is done using the unary operator (*) asterisk that always returns the value of the variable that is being located at the address which is specified by its operand.
E.g.:
#include <stdio.h> #include <conio.h> int main () { int variable = 30; /* actual variable declaration (step 1) */ int *ptr; /* pointer variable declaration (step 2) */ ptr = &variable; /* store address of var in pointer variable (step 3) */ printf("The address of the variable is: %x\n", &variable ); /* address stored in pointer variable */ printf("The address stored in the ptr variable is: %x\n", ptr ); /* access the value using the pointer */ printf("The value of the pointer *ptr variable is: %d\n", *ptr ); return 0; }
Output
Uses of a pointer in the applications
Pointers are one of the essential operations and have many applications in the programming language such as;
Functions, structure, and arrays.
Pointers help reduce the code; that is, it diminishes the lines of code required to run an operation. It is most used in functions, arrays, and structures as well.
Dynamic memory allocation.
In the C standard, there exists a memory allocation that involve dynamically allocating the memory. It uses the pointer theory in them.
Syntax
Below is Syntax for the dynamic memory allocation:
malloc() - “ptr = (data type*) malloc(byte-size);” calloc() - “ptr = (data type*) calloc(n, element-size);”
Note: free(), and realloc() requires the concept of pointer inorderfor them to be implemented.
Advantages of using the pointer
- A programmer can return the multiple values from the function using the pointer.
- Using a pointer drastically reduces the code and constantly improves the performance, and it is also used to revive the trees, string, and so on. They can also be used in concepts of arrays, functions, and structures.
- It makes a developer access the memory location at any point in time from the computer’s memory.
Disadvantages of using a pointer
- Segmentation fault is caused due to uninitialized pointers.
- Dynamically allocated block in the C standard should be freed explicitly. Else, it will lead to a memory leak.
- The pointers are usually slower than the normal variables.
- If pointers are updated with incorrect values, it might lead to memory corruption.