Adding Two Numbers Using Pointers

CCBeginner
Practice Now

Introduction

In C programming, pointers are used to handle the addresses of variables. Here, we will learn how to add two numbers using pointers in C programming.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") subgraph Lab Skills c/output -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} c/variables -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} c/data_types -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} c/operators -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} c/user_input -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} c/memory_address -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} c/pointers -.-> lab-123192{{"`Adding Two Numbers Using Pointers`"}} end

Declare the variables

We will begin our program by declaring three variables; two integer variables that will store the user input, and one integer pointer variable that will be used to point to the two integer variables. Here's the code to do that:

int first, second, *p, *q, sum;

Get user input

Now we need to get two integers from the user. We will use the scanf() function to do that as shown below:

printf("Enter two integers to add using pointers: ");
scanf("%d %d", &first, &second);

Point to the variables

Next, we declare two pointers that will point to the integer variables we just declared (first and second). We will use the & operator to get the address of the integer variables and store them in the pointers as shown below:

p = &first;
q = &second;

Add the numbers using pointers

Now that we have the integers pointed to, we can add them together using pointers to access their values. Here's the code to do that:

sum = *p + *q;

In the above code, the * operator gets the value stored at the addresses pointed to by p and q (which are first and second integers).

Print the result

Finally, we print the result using the printf() function as shown below:

printf("The sum of the entered numbers is: %d", sum);

Summary

In this lab, we learned how to add two numbers using pointers in C programming. We declared two integer variables and one integer pointer variable that points to the integers. We then got the values of the integers using scanf(), and then used pointers to add them together. Finally, we printed the sum using printf().

Other C Tutorials you may like