Javatpoint Logo
Javatpoint Logo

Add two numbers using Pointer in C

Pointers are a fundamental concept in programming languages that allow for efficient memory management and direct access to memory locations. Pointers are variables that store the memory address of another variable, rather than its value. This allows for various operations to be performed on the memory location directly, rather than through a copy of the data. One of the most important uses of pointers is in dynamic memory allocation. This is the process of allocating memory at runtime, rather than at compile-time. Pointers allow for the creation of new objects on the heap, and the deletion of objects no longer needed. This is useful for creating data structures such as linked lists and trees, which would not be possible without pointers.

Another important use of pointers is in passing variables to functions by reference. This means that a pointer to the variable is passed to the function, rather than a copy of the variable. This allows the function to make changes to the original variable, rather than just a copy. This can improve performance in some cases, as well as allowing for more complex functionality, such as recursion. Pointers are also used in low-level programming, such as operating systems and device drivers. They allow for direct manipulation of memory and can provide more control over the hardware.

However, pointers can also introduce complexity and potential errors if not used correctly. Memory leaks and null pointer references are common errors that can occur when working with pointers. Therefore, it is important to understand the proper usage of pointers and to implement proper error handling to avoid these issues. Overall, pointers are a powerful tool in programming languages, they allow for efficient memory management, direct access to memory locations, and enable advanced features such as linked lists, trees and other data structures, recursion and also used for low-level programming such as operating systems and device drivers.

C Code

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 an example of how to use pointers to add two numbers in C:

C Code

Output

Sum of 10 and 20 is: 30

Explanation:

the sum is printed using the printf statement, showing that the sum of 10 and 20 is 30. It's important to note that, in this example, we don't need to use pointers to add two numbers, we can directly add two variables without pointers. but the example is to show how to use pointers for basic operations. In this code, two variables num1 and num2 are declared with the values of 10 and 20 respectively.

Two pointers ptr1 and ptr2 are also declared and assigned the memory addresses of num1 and num2 respectively. The * operator is used to retrieve the values stored at the memory addresses stored in ptr1 and ptr2, which are 10 and 20 respectively. These values are then added together and stored in the variable sum. Finally, the sum is printed using the printf statement, showing that the sum of 10 and 20 is 30. The example is to demonstrate how to use pointers for basic operations.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA