Addition of two numbers using function pointers in C language
Introduction
Programming languages depend on pointers, a basic idea that enables effective memory management along with quick accessibility to memory locations. Instead of storing a variable's value, pointers save the memory location of another variable. This makes it possible to carry out different operations directly on the memory address as opposed to using a duplicate of the data. The dynamic allocation of memory process is one of the most significant uses of pointers. This is how memory is allocated at runtime in contrast to compile time. On the heap, pointers enable the development of new objects and the removal of ones that are no longer required. Without pointers, it would be impossible to create data structures like linked lists as well as trees. Therefore, this is helpful.
When working with pointers, frequent mistakes that might happen include null pointer references and memory leaks. To prevent these problems, it is crucial to comprehend how pointers should be used and to use appropriate error handling. Pointers are a crucial component in programming languages; they facilitate direct having access to the locations of memory, effective memory management, along with the utilization of advanced features like recursion, linked lists, trees, and other data structures. Low-level programming is also possible with pointers, as they are employed in operating systems along with device drivers.
Logic to Use Functions and Pointers to Add Two Numbers
We request two integers from the user, which are then stored in the addresses of variables x and y. The address of variable z, along with the values of variables x and y, are then sent to the addition() method and after that, display the result within the main method.
The goal is to modify the value that is presently at variable z's address. In this case, we may display the final results inside the main method itself regardless of the necessity of waiting for our function addition() to deliver a value to the calling function (main()).
The values supplied as the initial two arguments are added within the addition() method, and then the end result is stored within the address location that was supplied as the third parameter to the addition() function. Function addition() contains void as its return type, as it never returns any value.
Example 1:
#include <stdio.h>
int main()
{
int p = 5;
int q = 7;
int *pntr1 = &p;
int *pntr2 = &q;
int res;
res = *pntr1 + *pntr2;
printf("Addition of %d and %d is: %d \n", *pntr1, *pntr2, res);
return 0;
}
Output:
Explanation:
The printf command is used to print the total, which indicates that the addition of 5 and 7 is 14. It's essential to remember that, in this example, adding two variables directly avoids the requirement for pointers when adding two integers. However, the example demonstrates how to employ pointers for fundamental operations. Two variables, p and q, have been defined in this code, and the values that correspond to them are 5 and 7.
Additionally, two pointers, pntr1 and pntr2, are defined and given the corresponding memory locations, p and q. The values kept at the memory locations 5 and 7, respectively, that are stored in pntr1 and pntr2, may be retrieved using the * operator. The variable total is then created by adding these values together. Lastly, the printf instruction is used to print the sum, indicating that the product of 5 and 7 equals 14. The purpose of the example is to demonstrate how to make use of pointers for fundamental operations.
Example 2:
#include <stdio.h>
int add (int, int);
int main ()
{
int p, q, r;
printf ("Enter the first number p : ");
scanf ("%d", &p);
printf ("Enter the second number q : ");
scanf ("%d", &q);
int (*addPointr) (int, int);
addPointr = add;
r = addPointr (p, q);
printf ("The addition of two numbers is : %d \n", r);
return 0;
}
int add (int x, int y)
{
return x + y;
}
Output:
Example 3:
#include <stdio.h>
int solve (int *x, int *y)
{
int temp = *x + *y;
*y = *x - *y;
*x = temp;
}
int main ()
{
int x = 10, y = 5;
solve (&x, &y);
printf ("x + y = %d", x, y);
}
Output:
- Create the solve() function, which will accept the addresses of x and y.
- temp = total of the variables' values whose addresses are provided
- y = the variation in the variable's values whose addresses are provided
- x = temp
Conclusion
In conclusion, functions are a crucial part of C programming. They can be used to break up complex issues into smaller, easier-to-manage code parts. Above are the methods to use the function pointers to add two numbers in C language.