Answers/Solutions to Exercises in Chapter 3, Exercise 3

E3: Consider an int variable x and consider a pointer float* p=(float*)&x pointing to x. If we store a float value 2.35 in x directly using x=2.35 or indirectly using *p=2.35, will we get the same bit pattern stored in x in both cases, or will we get different patterns?

A3: Of course, we will get different bit patterns. Since x is defined as int, the execution of the statement x=2.35 will first "convert" 2.35 to an integer value 2 and the binary code for an integer value 2 will be stored in the memory location for x. Since p is defined as float*, the execution of *p=2.35 will store the binary code for the real value 2.35 in the location p points to, i.e. in x.

Back to Answers/Solutions Index                          Back to Answers/Solutions for Chapter 3 Index