metersToFeetAndInches puzzle

Has anyone noticed that if we changed *ftPtr = feet and *inPtr = inches to ftPtr = &feet and inPtr = &inches respectively, ftPtr local variable still refers to 9 feet and inPtr refers to 10.12 inches. But the output is 0 feet and 0 inches?
Does anyone have an explanation why?

#include <stdio.h>
#include <math.h>

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr){
    double rawFeet = meters * 3.281;
    unsigned int feet = (unsigned int)floor(rawFeet);
    if (ftPtr) {
        *ftPtr = feet;
        printf("ftPtr refers to %d\n", *ftPtr);
    }
    
    double fractionalFoot = rawFeet - *ftPtr;
    double inches = fractionalFoot * 12.0;
    if (inPtr) {
        *inPtr = inches;
        printf("inPtr refers to %.2f\n", *inPtr);
    }
}

int main(int argc, const char * argv[]) {
    double meters = 3.0;
    unsigned int feet;
    double inches;
    metersToFeetAndInches(meters, &feet, &inches);
    printf("feet: %d\ninches: %.2f\n", feet, inches);
    return 0;
}

The output:

ftPtr refers to 9
inPtr refers to 10.12
feet: 9
inches: 10.12
Program ended with exit code: 0

#include <stdio.h>
#include <math.h>

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr){
    double rawFeet = meters * 3.281;
    unsigned int feet = (unsigned int)floor(rawFeet);
    if (ftPtr) {
        ftPtr = &feet;
        printf("ftPtr refers to %d\n", *ftPtr);
    }
    
    double fractionalFoot = rawFeet - *ftPtr;
    double inches = fractionalFoot * 12.0;
    if (inPtr) {
        inPtr = &inches;
        printf("inPtr refers to %.2f\n", *inPtr);
    }
}

int main(int argc, const char * argv[]) {
    double meters = 3.0;
    unsigned int feet;
    double inches;
    metersToFeetAndInches(meters, &feet, &inches);
    printf("feet: %d\ninches: %.2f\n", feet, inches);
    return 0;
}

The output:

ftPtr refers to 9
inPtr refers to 10.12
feet: 0
inches: 0.00
Program ended with exit code: 0

[quote][code]
void metersToFeetAndInches (double meters, unsigned int * ftPtr, double * inPtr){
double rawFeet = meters * 3.281;
unsigned int feet = (unsigned int)floor(rawFeet);
if (ftPtr) {
ftPtr = &feet;
printf (“ftPtr refers to %d\n”, *ftPtr);
}

double fractionalFoot = rawFeet - *ftPtr;
double inches = fractionalFoot * 12.0;
if (inPtr) {
    inPtr = &inches;
    printf ("inPtr refers to %.2f\n", *inPtr);
}

}
[/code][/quote]
You are wiping out the original values of the pointer parameters by assigning to them the addresses of the local variables. Those pointer parameters are meant to contain the addresses of variables outside the function.

The meanings of the assignment statements below are totally different.

void metersToFeetAndInches (double meters, unsigned int * ftPtr, double * inPtr) {
...
    unsigned int feet = (unsigned int)floor(rawFeet);
    if (ftPtr) {
        ftPtr = &feet;    // this changes the value of ftPtr, consequently wipes out the original address value passed in
        ...
    }
...
}
void metersToFeetAndInches (double meters, unsigned int * ftPtr, double * inPtr) {
...
    unsigned int feet = (unsigned int)floor(rawFeet);
    if (ftPtr) {
        * ftPtr = feet; // this changes the value of the object whose address is passed in ftPtr.
        ...
    }
...
}

I think the book is asking you to pass the computed values, feet and inches, through the pointers back to the caller.

[Become a competent programmer: pretty-function.org]

To ibex10.
Thanks a lot for your excellent explanation.
I was experimenting with the code and bumped into this problem.
Now it’s clear.