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
Functions With Parameters and No Return Values Quiz by CodeChum Admin 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 TTTT
int mystery (int u, int v) { int a; a = u - V; u = a; V = u; return u + v; choose the output of the follos cout << mystery (9,7); Select one: a. 7 b. 4 C. 9
After the following code is executed, what is the value of i? int i;int *p;p = &i;i = 1;*p = 2;
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning