Tuesday, 15 March 2016

Is the following valid code? If so, what does it print? cout << (int *) “Home of the jolly bytes”;

Leave a Comment

Question:

 Is the following valid code? If so, what does it print?              

cout << (int *) “Home of the jolly bytes”;

Answer:

Ye s , it is valid.The expression "Home of the jolly bytes" is a string constant; hence it evaluates as the address of the beginning of the string.The cout object interprets the address of a char as an invitation to print a string, but the type cast (int *) converts the address to type pointer-to-int, which is then printed as an address. In short, the statement prints the address of the string, assuming the int
type is wide enough to hold an address.

Source:

C++ Primer Plus Sixth Edition Chapter Review

If You Enjoyed This, Take 5 Seconds To Share It