UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to find the sum of two numbers using pointers

C program for how find the sum of two numbers using pointers

by Krishna viswambharan


Posted on 17 Oct 2017 Category: C Views: 172

Edited on 12 Apr 2018


C program to find the sum of two numbers using pointers

In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer. Declaration of Pointer pointer is given as :-

data_type* pointer_variable_name;

 eg: int* p;  //pointer to an integer

The asterisk * used to declare a pointer

 The concept of pointer in C is clearly explained in the below example.

Let , int a=50;

         int *ptr;

         ptr=&a  // this statement means that address of ‘a’ is assigned to pointer ‘ptr’.

        printf(“%d”,*ptr);   // this statement display a’s value using pointer variable ,ptr

Below is a C program to find sum of two numbers using pointers.

#include <stdio.h>
#include <conio.h>
void main()
{
    int n1,n2,sum=0;
    int *ptr1,*ptr2;
    printf(“Enter the first number :”);
    scanf(“%d”,&n1);
    printf(“Enter the second number :”);
    scanf(“%d”,&n2);
    ptr1=&n1;  //address of n1 is assigned to ptr1
    ptr2=&n2;     //address of n2 is assigned to ptr2
    sum=*ptr1+*ptr2;    
    printf(“sum  =%d”,sum);
    getch();
}

output

 

 



Leave a Comment:


Advertisement