Addition using pointers

This program performs addition of two numbers using pointers. We have two integer variables x, y and two pointer variables p and q. First we assign the addresses of x and y to p and q respectively and then assign the sum of x and y to variable sum. Note that & is address of operator and * is value at address operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main()
{
// declare 3 variables and 2 pointers
int x, y, *p, *q, sum;

// take input and store inputs into variables at addresses
printf("Enter two integers to add\n");
scanf("%d%d", &x, &y);

// point the pointers at the addresses of the stored variables
p = &x;
q = &y;

// add the values that the pointers point at
sum = *p + *q;

// print the sum
printf("Sum of entered numbers = %d\n",sum);

return 0;
}
^