C Program to Add Two Numbers Using Pointers

Introduction

In C programming, pointers are used to store and manipulate memory addresses. By using pointers, you can directly access and perform operations on variables at their memory locations. This guide will show you how to write a C program to add two numbers using pointers.

Example:

  • Input: Two numbers, say 10 and 20.
  • Output: The sum of the two numbers, which is 30.

Problem Statement

Create a C program that:

  • Declares two integer variables and initializes them.
  • Uses pointers to access and add the values of these variables.
  • Displays the result of the addition.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare and Initialize Variables: Declare two integer variables and initialize them with values.
  3. Declare Pointer Variables: Declare two pointer variables to hold the addresses of the integer variables.
  4. Assign Addresses to Pointers: Use the address-of operator (&) to assign the addresses of the variables to the pointers.
  5. Add the Values Using Pointers: Use the dereference operator (*) to access the values pointed to by the pointers and add them.
  6. Display the Result: Use printf to display the result of the addition.

C Program to Add Two Numbers Using Pointers

#include <stdio.h>

int main() {
    // Step 2: Declare and initialize variables
    int num1 = 10, num2 = 20;

    // Step 3: Declare pointer variables
    int *ptr1, *ptr2;

    // Step 4: Assign addresses to pointers
    ptr1 = &num1;
    ptr2 = &num2;

    // Step 5: Add the values using pointers
    int sum = *ptr1 + *ptr2;

    // Step 6: Display the result
    printf("The sum of %d and %d is: %d\n", *ptr1, *ptr2, sum);

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Declare and Initialize Variables

  • The variables num1 and num2 are declared as integers and initialized with the values 10 and 20, respectively.

Step 3: Declare Pointer Variables

  • The pointer variables ptr1 and ptr2 are declared to hold the addresses of the integer variables.

Step 4: Assign Addresses to Pointers

  • The address of num1 is assigned to ptr1, and the address of num2 is assigned to ptr2 using the address-of operator (&).

Step 5: Add the Values Using Pointers

  • The values pointed to by ptr1 and ptr2 are accessed using the dereference operator (*). These values are then added, and the result is stored in the variable sum.

Step 6: Display the Result

  • The program uses printf to display the result of the addition, showing both the original numbers and their sum.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

The sum of 10 and 20 is: 30

Conclusion

This C program demonstrates how to use pointers to add two numbers. It covers basic concepts such as pointer declaration, using the address-of operator to assign addresses to pointers, and using the dereference operator to access the values stored at those addresses. This example is useful for beginners learning how to work with pointers in C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top