Pointer in C++:
A pointer is a variable whose value will be another variable’s address. Generally, a pointer variable is declared as follows:
type *var-name;
Here, type is the pointer’s base type and var-name is the pointer variable name. The asterisk is used to designate a variable as a pointer.
Given Program:
//Include libraries
#include <iostream>
//Use namespace
using namespace std;
//Define main method
int main()
{
// Declare pointer variables
int *p1, *p2;
//Assign value
p1 = new int;
//Assign value
p2 = new int;
//Assign value
*p1 = 10;
//Assign value
*p2 = 20;
//Display value
cout << *p1 << " " << *p2 << endl;
//Assign value
p1 = p2;
//Display value
cout << *p1 << " " << *p2 << endl;
//Assign value
*p1 = 30;
//Display value
cout << *p1 << " " << *p2 << endl;
//Pause console window
system("pause");
//Return 0 value
return 0;
}
Pointer in C++:
A pointer is a variable whose value will be another variable’s address. Generally, a pointer variable is declared as follows:
type *var-name;
Here, type is the pointer’s base type and var-name is the pointer variable name. The asterisk is used to designate a variable as a pointer.
Given Program:
//Include libraries
#include <iostream>
//Use namespace
using namespace std;
//Define main method
int main()
{
// Declare pointer variables
int *p1, *p2;
//Assign value
p1 = new int;
//Assign value
p2 = new int;
//Assign value
*p1 = 10;
//Assign value
*p2 = 20;
//Display value
cout << *p1 << " " << *p2 << endl;
//Assign value
p1 = p2;
//Display value
cout << *p1 << " " << *p2 << endl;
//Assign value
*p2 = 30;
//Display value
cout << *p1 << " " << *p2 << endl;
//Pause console window
system("pause");
//Return 0 value
return 0;
}
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Problem Solving with C++ (10th Edition)
- Write the output of the following code:mister_pet = Pet("Master", "Dog")mister_dog = Dog("Mister", True)mister_cat = Cat("Tom",False)mister_cat_2 = Cat("Bully", True)print(mister_dog.getName())print(mister_pet.getName())print(mister_cat.getName())print(mister_cat_2.hatesDogs())print(mister_cat.hatesDogs())arrow_forwardConsider the following code. What will be printed out by the code?arrow_forwardWhat is the output of the following code segment? int n = 1;do { cout << n << ' '; n++; } while (n >= 5) What is the output of the following code segment? int n = 1;do { cout << n << ' '; n++; } while (n <= 5);arrow_forward
- trace the following code and give the output. for (int a = 1; a <= 3; a++) for (int b = 1; b <= 3; b++) cout << a+b << endl;arrow_forwardint const MULTIPLIER = 5; is a valid way to declare a constant integer variable. Group of answer choices True False ------- Given the following code segment, what is output to the screen? int num1 = 6; int num2 = 4 * num1++; cout << "num1=" << num1 << " num2=" << num2; Group of answer choices num1=7 num2=24 num1=6 num2=24 num1=6 num2=28 Nothing, because the code does not compile.arrow_forward#include main() {int=a, b=2, x=0, x=a + b * a + 10/2 * a, printf("value is =%d", x);} What is the value?arrow_forward
- Give solution in C ++ Language with secreenshoot of source code. Part 01In this task, you need to do the following:• Write a function named displayMessage() that takes user name as input in character array and then shows greetings• Now take the name input in main() and pass the name as an argument to displayMessage() function• Change the displayMessage() method such that it returns the number of characters after displaying the greetings part 02Write a function power that takes two parameters a and b. And it returns the power as ab.arrow_forwardint j; for (int i = 0; i < 5; i++) {j = 2*j*i;} What is j?arrow_forwardWhat is the output of the following code? int *intArrayPtr;int *temp;intArrayPtr = new int[5];*intArrayPtr = 7;temp = intArrayPtr;for (int i = 1; i < 5; i++){intArrayPtr++;*intArrayPtr = *(intArrayPtr - 1) 1 2 * i;}intArrayPtr = temp;for (int i = 0; i < 5; i++){cout << *intArrayPtr << " ";intArrayPtr++;}cout << endl;arrow_forward
- Create a program that accepts an integer N, and pass it to the function generatePattern. generatePattern() function which has the following description: Return type - void Parameter - integer n This function prints a right triangular pattern of letter 'T' based on the value of n. The top of the triangle starts with 1 and increments by one down on the next line until the integer n. For each row of in printing the right triangle, print "T" for n times. In the main function, call the generatePattern() function. Input 1. One line containing an integer Output Enter N: 4 T TT TTT TTTTarrow_forwardWhat is the output of the following code? for (int k = 2; k <= 10000; k = k * k)cout << k << " ";cout << endl;arrow_forwardWhat is the output of the following code? int num = 17; //Line 1double gpa = 3.85; //Line 2bool done; //Line 3done = (num == static_cast<int>(2 * gpa + 9.3)); //Line 4cout << "The value of done is: " << done << endl; //Line 5arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education