Add Two Numbers Using Pointer In C

Nevertheless, pointers can add intricacy and potential mistakes if mishandled. Common errors like memory leaks and null pointer references can arise when dealing with pointers. Therefore, grasping the correct utilization of pointers and incorporating effective error management is crucial to prevent such issues. In essence, pointers serve as a robust asset in programming languages, facilitating efficient memory control, direct memory access, and enabling sophisticated functionalities like linked lists, trees, various data structures, recursion, as well as being essential in low-level programming applications such as operating systems and device drivers.

C Code

Example

#include <stdio.h>

int main() {
    int x = 10;
    int *ptr; // Declare a pointer to an integer

    ptr = &x; // Assign the address of x to the pointer

    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", &x);
    printf("Value of ptr: %p\n", ptr);
    printf("Value stored at the address stored in ptr: %d\n", *ptr);

    *ptr = 15; // Change the value stored at the address stored in ptr

    printf("\nAfter changing the value stored at the address stored in ptr\n");
    printf("Value of x: %d\n", x);
    printf("Value stored at the address stored in ptr: %d\n", *ptr);

    return 0;
}

Output

Output

Value of x: 10
Address of x: 0x7ffeeb5e1c7c
Value of ptr: 0x7ffeeb5e1c7c
Value stored at the address stored in ptr: 10
After changing the value stored at the address stored in ptr
Value of x: 15
Value stored at the address stored in ptr: 15

Add two numbers using Pointer in C

Here is a demonstration of utilizing pointers to perform addition of two numbers in the C programming language:

C Code

Example

#include <stdio.h>

int main() {
    int num1 = 10;
    int num2 = 20;
    int *ptr1 = &num1;
    int *ptr2 = &num2;
    int sum;

    sum = *ptr1 + *ptr2;

    printf("Sum of %d and %d is: %d\n", *ptr1, *ptr2, sum);

    return 0;
}

Output

Output

Sum of 10 and 20 is: 30

Explanation:

The addition result is displayed by the printf function, demonstrating that the addition of 10 and 20 equals 30. It's crucial to emphasize that pointers are not required in this instance to sum two values; we can add two variables directly without pointers. However, this illustration serves to illustrate the application of pointers in fundamental computations. Within this code snippet, two variables, num1 and num2, are initialized with the values 10 and 20 correspondingly.

Two pointers, ptr1 and ptr2, are initialized with the memory addresses of num1 and num2. The * symbol is employed to access the values stored at the memory locations pointed to by ptr1 and ptr2, yielding 10 and 20 respectively. These retrieved values are summed up and saved in the variable sum. Subsequently, the printf function is used to display the sum, indicating that the addition of 10 and 20 results in 30. This illustration serves to showcase the utilization of pointers for fundamental computations.

Input Required

This code uses input(). Please provide values below: