Why can't we store values using pointer at the function?


#1

why doesn’t the code work and I’m getting error like
Thread 1:EXC_BAD_ACCESS(code=1, address=0x0)
Incompatible integer to pointer conversion passing 'unsigned to parameter of type 'double *'
here’s the code


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

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr){
    //Convert meters into decimal value of feet and inches
    double feet_and_inches= meters*3.218;
    // Seperate the feet and inches value from the decimal value
    double inches = modf(feet_and_inches,*ftPtr);
    //Store value of inches at inPtr
     double inches*=12.0
    *inPtr=inches;
    
    
    
    
}


int main(int argc, const char * argv[])
{
    double meters=3.0;
    unsigned int feet;
    double inches;
    
    metersToFeetAndInches(meters, &feet, &inches);
    printf("%.1f meters is equal to %d feet and %.1f inches.", meters, feet, inches);
 
    return 0;
}

#2

[quote]Incompatible integer to pointer conversion passing ‘unsigned to parameter of type 'double *
[/quote]
Read the manual page for the modf function and find out what type of argument you can pass for the second parameter.

The type of the second argument should be double *, not unsigned int.


#3

Why can’t this works now?
I mean I’ve already set double*ftPtr


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

void metersToFeetAndInches(double meters, double *ftPtr, double *inPtr){
    //Convert meters into decimal value of feet and inches
    double feet_and_inches= meters*3.218;
    // Seperate the feet and inches value from the decimal value
    double inches = modf(feet_and_inches, *ftPtr)*12.0;
    *inPtr=inches;
    
    
    
    
    
}


int main(int argc, const char * argv[])
{
    double meters=3.0;
    double feet;
    double inches;
    
    metersToFeetAndInches(meters, &feet, &inches);
    printf("%.1f meters is equal to %f feet and %.1f inches.", meters, feet, inches);
 
    return 0;
}

#4

Still not correct. This time you are passing a double, not a double *.

The second argument of modf should be a double *.

Correct:

void metersToFeetAndInches (double meters, double *ftPtr, double *inPtr) { ... modf (..., ftPtr) ... // passing a double * since ftPtr is a double * } Incorrect:

void metersToFeetAndInches (double meters, double *ftPtr, double *inPtr)
{
   ... modf (..., *ftPtr) ...   // passing a double since ftPtr is a double *
}

#5

But I always thought that to assign value to an address we should do something like this

*addressofI=89

this is the way to assign address , i was thinking to use the same way to assign value to interger into *ftPtr
like this

modf(feet_and_inches, *ftPtr);

Isn’t that replacing *ftPtr with ftPtr means that we’re storing the value of int and replacing the address that was assigned to it earlier?

void metersToFeetAndInches(double meters, double *ftPtr, double *inPtr)
//since we already assigned the address to *ftPtr and *inPtr, double*ftPtr=&feet, double*inPtr=&inches
//so to assigned value of something to the address &feet, we need to use this way as shown in the book
//frationPart=modf(pi, $integerPart) so I implemented the same method to assign value to the address with 
//some minor amendments.
//modf(feet_and_inches, *ftPtr)< the value of int will be assigned to the address &feet
//By *ftPtr=(value of int) as value of &feet has been assigned to *ftPtr, double *ftPtr=&feet
 metersToFeetAndInches(meters, &feet, &inches);