Add two numbers using pointers with C.
// Perform arithmetic operation //
sum = (*ptr1) + (*ptr2);
diff = (*ptr1) - (*ptr2);
mult = (*ptr1) * (*ptr2);
div = (*ptr1) / (*ptr2);
#include <stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2;
ptr1 = &num1; // ptr1 stores the address of num1
ptr2 = &num2; // ptr2 stores the address of num2
printf("Enter any two numbers: ");
scanf("%d%d", ptr1, ptr2);
sum = *ptr1 + *ptr2;
printf("Sum = %d", sum);
return 0;
}
Output
Enter any two numbers : 5 10
Sum = 15
0 Comments