C Program to Add Two Numbers Using Pointers

  • Write a program in c to add two numbers using pointers.

A variable in C is the name given to a memory location, where a program can store data. Instead of referring a variable’s data with it’s identifier we can also use memory address to access it using ‘*'(value of) operator. To get the memory address of any variable we can use ‘&'(Address Of) Operator.

This program does addition of two numbers using pointers. First, we take two integers as input form user and store it in firstNumber and secondNumber integer variables then we assign addresses of firstNumber and secondNumber in firstNumberPointer and secondNumberPointer integer pointer variable respectively using Address operator(&). Now we add the values pointed by firstNumberPointer and secondNumberPointer using Value at operator (*) and store sum in variable sum. At last, prints the sum on screen using printf function.

Pointer Operators in C

Operator Operator name Description
* Value at Operator Returns the value of the variable located at the address specified by the pointer
& Address of operator Returns the memory address of a variable

C Program to Add Two Numbers using Pointer

C Program to Add Two Numbers using Pointer 1

/*
* C Program to Add two numbers using pointers
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
     
    int firstNumber, secondNumber, sum;
    /* Pointers declaration */
    int *firstNumberPointer, *secondNumberPointer;
    printf("Enter two numbers \n");
    scanf("%d %d", &firstNumber, &secondNumber);
    /* Pointer assignment*/
    firstNumberPointer = &firstNumber;
    secondNumberPointer = &secondNumber;
     
    sum = *firstNumberPointer + *secondNumberPointer;
    printf("SUM = %d", sum);
    getch();
    return 0;
}

Program Output

Enter two numbers 
4 6
SUM = 10