Problem Solving With C++ (Looseleaf) - With Access
Problem Solving With C++ (Looseleaf) - With Access
9th Edition
ISBN: 9780133835267
Author: SAVITCH
Publisher: PEARSON
Question
Book Icon
Chapter 9.1, Problem 4STE
Program Plan Intro

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;

}

Program Plan Intro

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;

}

Blurred answer
Students have asked these similar questions
what will be the output? #include int main () {int i = 3; i++; print ("%d", i); i--; print ("%d", i); return 0;} a. 34, b. 33, c. 44, d. 43
In Java: Develop a void function that takes two integers and prints whether the first number is divisible by the second. For example, with arguments 51 and 17, the function should print “51 is divisible by 17”. With arguments 17 and 8, the function should print “17 is not divisible by 8”. Call the function twice with different arguments to show each possible result. What happens when the second argument is greater than the first?
what will be the output? #include int main() {int i = 3; i++; printf ("%d", i); i--, printf("%d", i); return 0;}
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education