C++ Primer ex 4.16

A

arnuld

/* C++ Primer - 4/e

* exercise - 4.16
*/

#include <iostream>


int main()
{
int i = 2;
int j = -3;

int* p1 = &i;
int* p2 = &j;

std::cout << "*p2 = *p2 * *p1 -> "
<< (*p2 = *p2 * *p1)
<< std::endl;

std::cout << "*p1 *= *p1 -> "
<< (*p2 *= *p1)
<< std::endl;

return 0;
}

/* OUTPUT

[arnuld@arch cpp $] g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp $] ./a.out
*p2 = *p2 * *p1 -> -6
*p1 *= *p1 -> -12
[arnuld@arch cpp $]

*/


i do not understand the outputs. "*p2 = *p2 * *p1" outputs:

j = j * i (lavlues)

or

-3 = 2 * -3 (rvalues)
 
A

Abdo Haji-Ali

/* C++ Primer - 4/e

* exercise - 4.16
*/

#include <iostream>

int main()
{
int i = 2;
int j = -3;

int* p1 = &i;
int* p2 = &j;

std::cout << "*p2 = *p2 * *p1 -> "
<< (*p2 = *p2 * *p1)
<< std::endl;

std::cout << "*p1 *= *p1 -> "
<< (*p2 *= *p1)
<< std::endl;

return 0;
}

/* OUTPUT

[arnuld@arch cpp $] g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp $] ./a.out
*p2 = *p2 * *p1 -> -6
*p1 *= *p1 -> -12
[arnuld@arch cpp $]

*/

i do not understand the outputs. "*p2 = *p2 * *p1" outputs:

j = j * i (lavlues)

or

-3 = 2 * -3 (rvalues)

Neither, it means (literally):
"*p2 = *p2 * *p1"

Anything that appears inside two double quotations would appear AS IS
in the output:
So "*p2 = *p2 * *p1" would appear as simply "*p2 = *p2 * *p1"
That's why it's called a "String Literal".

However,
std::cout << "*p2 = *p2 * *p1 -> "
<< (*p2 = *p2 * *p1) // <-- This line
<< std::endl;

Isn't a string literal it's an arithmatic expression that's computed
in real-time,
so the value in the memory location that the pointer p2 is pointing at
will be multiplied by the value in the memory location that the
pointer p1 is pointing at and the result is stored in the memory
location that the pointer p2 is pointing at (Phew... :). This result
is also printed to the output.

Abdo Haji-Ali
Programmer
In|Framez
 
V

Victor Bazarov

arnuld said:
/* C++ Primer - 4/e

* exercise - 4.16
*/

#include <iostream>


int main()
{
int i = 2;
int j = -3;

int* p1 = &i;
int* p2 = &j;

std::cout << "*p2 = *p2 * *p1 -> "
<< (*p2 = *p2 * *p1)
<< std::endl;

std::cout << "*p1 *= *p1 -> "
<< (*p2 *= *p1)
<< std::endl;

return 0;
}

/* OUTPUT

[arnuld@arch cpp $] g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp $] ./a.out
*p2 = *p2 * *p1 -> -6
*p1 *= *p1 -> -12
[arnuld@arch cpp $]

*/


i do not understand the outputs. "*p2 = *p2 * *p1" outputs:

j = j * i (lavlues)

or

-3 = 2 * -3 (rvalues)

I don't understand what exactly don't you understand. Are you
confusing the *assignment* operator and the *equality* operator?

*p2 = *p2 * *p1

is an expression that assigns to the contents of the memory to
which 'p2' points the value calculated from multiplying of the
contents to which 'p2' and 'p1' originally point. The final
value of the expression is the value that was assigned. IOW,
the expression can be rewritten as (for some imaginary 't1'
and 't2' temporary values of type 'int'):

(t1 = *p2, t2 = *p1, *p2 = t1 * t2, *p2)

, except there are no sequence points in the original expression.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C++ Primer ex 7.5 18
C++ Primer ex 7.6 17
question regarding the shared_ptr use_count 6
C++ Primer ex 3.14 8
C++ Primer ex 3.24 (bitset class) 5
C++ Primer ex 9.27 4
C++ Primer ex 7.12 2
C++ Primer ex 4.30 10

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top