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
10and20. - 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
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Declare and Initialize Variables: Declare two integer variables and initialize them with values.
- Declare Pointer Variables: Declare two pointer variables to hold the addresses of the integer variables.
- Assign Addresses to Pointers: Use the address-of operator (
&) to assign the addresses of the variables to the pointers. - Add the Values Using Pointers: Use the dereference operator (
*) to access the values pointed to by the pointers and add them. - Display the Result: Use
printfto 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
num1andnum2are declared as integers and initialized with the values10and20, respectively.
Step 3: Declare Pointer Variables
- The pointer variables
ptr1andptr2are declared to hold the addresses of the integer variables.
Step 4: Assign Addresses to Pointers
- The address of
num1is assigned toptr1, and the address ofnum2is assigned toptr2using the address-of operator (&).
Step 5: Add the Values Using Pointers
- The values pointed to by
ptr1andptr2are accessed using the dereference operator (*). These values are then added, and the result is stored in the variablesum.
Step 6: Display the Result
- The program uses
printfto 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.