-1
float number = 1.0;
float* number_pointer = &number;
int* int_pointer = (int*) number_pointer;                  
*int_pointer += 30;
printf("%f \n",number);

What does the line (*int_pointer)+= 30; do here?

Unome
  • 6,750
  • 7
  • 45
  • 87
  • 2
    By casting like that you're breaking [the strict aliasing rule](http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). And using the pointer to `int` you will end up with undefined behavior, floating point values and integer values have different encodings in memory. To answer your question: The expression `*int_pointer += 30` does bad things, and may even cause [nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html) – Some programmer dude Aug 12 '15 at 17:14
  • Many thanks. Will look into that strict aliasing rule – user5220361 Aug 12 '15 at 17:18

2 Answers2

1

I see two questions there.

  1. What happens when you use

    int* int_pointer = (int*) number_pointer;
    

    That line takes the address of an object that holds a float and stores that address in int_pointer. At this point, the compiler will be able to treat the address as though it holds an integer. That constitutes a violation of the strict aliasing rule.

  2. What happens when you use

    *int_pointer += 30;
    

    It increments the value of the object that int_pointer points to by 30. In cases where int_pointer points to a valid int object, that operation will be fine. In your case, this will cause undefined behavior since int_pointer really points to an object of type float.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Every possible pattern of bits is a valid int, so the += 30 will not fail. It will just scramble the bits of the float, possibly into an invalid representation, but probably not--the low-order bits of a float are all mantissa, so changing them will probably generate a perfectly valid float. It just won't bear much relationship to the "30" you ostensibly added. – Lee Daniel Crocker Aug 12 '15 at 19:25
0

Let me explain each statement:

float number = 1.0;

Here you have declared and initialized a float variable called number.

float* number_pointer = &number;

Declared and initialized float type pointer number_pointer to number. This means number_pointer holds the address of the variable number and *number_pointer gives the value of the variable number, i.e., 1.0.

int* int_pointer = (int*) number_pointer;

Type-casted float pointer number_pointer to integer pointer and initialized to int_pointer. After this explicit type-casting, the value of *int_pointer is not 1.0 but its corresponding integer 1065353216.

*int_pointer += 30;

This is a short hand for *int_pointer = *int_pointer + 30;. *int_pointer becomes 1065353246.

printf("%f \n",number);

Prints the value of number, which is now 1.000004 and not the initial 1.000000. The change is caused by the addition performed on int_pointer which points to where number_pointer is pointing which in turn is the variable number.