C++ Programming Code Examples
C++ > Pointers Code Examples
C++ Programming Code to Add Two Numbers using Pointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* C++ Programming Code to Add Two Numbers using Pointer
To add two numbers using pointer in C++ Programming, you have to ask to the user to enter the two number, then make two pointer type variable of same type say *ptr1 and *ptr2 to initialize the address of both the number and make another variable say sum which contain the addition of the two number like sum = *ptr1 + *ptr2 and display the result on the screen. Here * is also called as value at address operator.
Following C++ program ask to the user to enter the two number and add the entered two number using pointer then display the addition result on the screen:
C++ Program - Add Two Numbers using Pointer */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int number1, number2, *ptr1, *ptr2, sum=0;
cout<<"Enter the two number :";
cin>>number1>>number2;
ptr1 = &number1;
ptr2 = &number2;
sum = *ptr1 + *ptr2;
cout<<"Sum of the two number is "<<sum;
getch();
}
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value. In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type. You should see a pointer as a symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also create and manipulate dynamic data structures. In C++, a pointer variable refers to a variable pointing to a specific address in a memory pointed by another variable.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
To find the frequency of character in string in C++, enter the string and enter the character to Find the Frequency of that character (or to "count the occurrence of character") in string
This C++ Program implement "Affine Cipher". The affine cipher is a type of Monoalphabetic Substitution Cipher, wherein each letter in an alphabet is mapped to its numeric equivalent,
Linking nodes in binomial heap. Create nodes in binomial heap and Insert nodes in binomial heap. Union nodes in "Binomial Heap". Merge nodes in binomial heap. And display binomial
This c++ program shows the greatest number between 3 numbers use 'if-else-if' statement. It takes three numbers as input from user and output the greatest number. Enter value first
C++ program ask to the user to enter the side length of a square to calculate and displaying the "area and perimeter" of the square. Enter length of a side of square and "calculate area"