Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter 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
int func(int a, int b) { return (a
int a = -2, b = 5, c = 1, d = 11, e = -5; int x = e + d - 2; a = b - c + x; b = e * x + 5; c = d / 3  - x; d = b + c - 7; e = a + c - x;   a =  b =  c =  d =  e =
What does the function f do? struct Point2D { double x; double y; struct Triangle { Point2D v1; Point2D v2; Point2D v3; }; void f(Triangle&t) { } int temp = 12.5; temp = t.v1.x; t.v1.x = t.v1.y; t.v1.y = temp; } int main () { Triangle mytri; mytri.v1.x = 1.0; mytri.v1.y = 22.5; f (mytri); Swaps values of x and y in vertex 1 of an argument of type Triangle Initializes value of x in vertex 1 of an argument of type Triangle Sets all x,y values in all vertices of an argument of type Triangle Swaps value of x in vertex 1 with value of x in vertex 2, for an argument of type
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