What is going on here?


Hello there!

I noticed a VERY small detail and it’s bugging me in the metersToFeetAndInches program (top of page 73)
I will list it here for reference sake.[code]#include <stdio.h>
#include <math.h>

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr)
// This function assumes meters in non-negative.

// Convert the number of meters into a floating-point number of feet.
double rawFeet = meters * 3.281; // e.g. 2.4536

// How many complet feet as an unsigned int?
unsigned int feet = (unsigned int)floor(rawFeet);

// Store the number of feet at the supplied address.
printf("1. - Storing %u to the address %p.\n", feet, ftPtr);
*ftPtr = feet;

// Calculate inches
double fractionalFoot = rawFeet - feet;
double inches = fractionalFoot * 12.0;

// Store the number of inches at the supplied address
printf("2. - Storing %.2f to the address %p.\n", inches, inPtr);
*inPtr = inches;


int main(int argc, const char * argv[])
double meters = 3.0;
unsigned int feet;
double inches;

metersToFeetAndInches(meters, &feet, &inches); /* calls the metersToFeetAndInches fuction */
printf("%.1f meters is equal to %d feet and %.1f inches.\n", meters, feet, inches);

return 0;

The answer is:[quote]1. - Storing 9 to the address 0x7fff5fbff7b4.
2. - Storing 10.12 to the address 0x7fff5fbff7a8.
3.0 meters is equal to 9 feet and 10.1 inches.[/quote]
(I numbered the 1st and 2nd printf to clarify my concern.)
We stored 9 in address 0x7fff5fbff7[color=#FF0000]b[/color]4 and 10.12 in address 0x7fff5fbff7[color=#FF0000]a[/color]8.

Yet 0x7fff5fbff7[color=#FF0000]a[/color]8 comes before 0x7fff5fbff7[color=#FF0000]b[/color]4. It is as if the compiler going from the bottom up with the memory address. I know it like a small detail but it might be significant. Is that how the compiler works?



[quote]int main (int argc, const char * argv[]) { double meters = 3.0; unsigned int feet; double inches; ... [/quote]
That’s because the local variables of a function are created on a stack. (The stack grows in a certain direction.)