Hi All,
I’m fairly new to C and Objective-C and was wondering why I get an error (Passing ‘double’ to parameter of incompatible type ‘const char *’) When I try to print out just a variable, yet that error goes away when I add a string to the print out.
Thank you in advance for the input.
ibex10
September 2, 2018, 12:37pm
2
I am very pleased to see that you are doing Objective-C!
As to what you are experiencing.
When calling a function, always pass arguments of compatible types, as expected by the function.
A function call must always be preceded by its declaration. The declaration specifies the types of the parameters the function expects to receive.
Here is a complete example:
#import <Foundation/Foundation.h>
// Declare functions before use
void printCString (const char *);
void printDoubleValue (const double);
int main (int argc, const char * argv[]) {
@autoreleasepool {
const char * str = "PI ~= 22.0/7";
printCString (str);
printDoubleValue (strlen (str));
printDoubleValue (M_PI);
printDoubleValue (22.0/7);
}
return 0;
}
void printCString (const char *str) {
NSLog (@"%s: %s", __func__, str);
}
void printDoubleValue (const double v) {
NSLog (@"%s: %f", __func__, v);
}
The above example produced the following output.
2018-09-02 21:59:17.289771+0930 Params[65720:7276979] printCString: PI ~= 22.0/7
2018-09-02 21:59:17.290167+0930 Params[65720:7276979] printDoubleValue: 12.000000
2018-09-02 21:59:17.290196+0930 Params[65720:7276979] printDoubleValue: 3.141593
2018-09-02 21:59:17.290218+0930 Params[65720:7276979] printDoubleValue: 3.142857