0

I am trying to answer this problem:

Write a function that is given an integer, n, by the main method where 1 ≤ n ≤ 9999 and prints whether it is even, odd, or/and prime. Write your answer in the form:
2 is even and prime

The code I have written so far is:

/*Lab2 ex4 */ #include <stdio.h> #include <math.h> #include <stdbool.h> bool IsPrime(int num) { int i; for (i=2; i<num; i++) { if (num%i == 0) { return false; } } return true; } bool IsEven(int num) { if (num%2 == 0) { return true; } else { return false; } } char BoolToString(bool prime, bool even) { if (prime == true, even == true) { char* result = "is even and prime"; return result; } else if (prime == true, even == false) { char* result = "is odd and prime"; return result; } else if (prime == false, even == true) { char* result = "is even and not prime"; return result; } else if (prime == false, even == false) { char* result = "is odd and prime"; return result; } else { char* result = "error"; return result; } } main() { printf("%d %c\n", 11, BoolToString(IsPrime(11), IsEven(11))); } 

But I get the error message return makes integer from pointer without a cast on the return statements in the BoolToString function.

I don't understand what I've done wrong here? Many thanks.

(I only started C about 2 weeks ago so apologies if I've done something in a horrible way or completely misunderstood how something is used.)

3
  • you declare the result as character pointer and the BoolToString function return type is char. So, it shows error Commented Oct 13, 2015 at 12:47
  • 3
    Just change the return type to char *BoolToString(). If you want to combine two booleans use &&, and use the bool values directly: if (prime == false, even == true) should be if (!prime && even) . ("If not prime and even".) The comma in that case is an operator which returns the value of the second expression, so that the first test is completely ignored. Interesting mistake which does not generate an error or even warning, does it? Commented Oct 13, 2015 at 12:48
  • And then try to be smart about how to combine the answer string from building blocks plus inserting "not" in strategic places, depending on the two conditions. Look into the string manipulation metods strcat and strcpy and their return values.-- Just combinatorically listing each combination feels so un-algorithmic :-). Just imagine you also want to output the number of digits... Commented Oct 13, 2015 at 13:01

5 Answers 5

6

You have to use char* return type to return data of char*.
Moreover, using const char* is better in this case because string literals are unmodifiable.

const char *BoolToString(bool prime, bool even) { if (prime == true && even == true) { return "is even and prime"; } else if (prime == true && even == false) { return "is odd and prime"; } else if (prime == false && even == true) { return "is even and not prime"; } else if (prime == false && even == false) { return "is odd and prime"; } else { return "error"; } } 

You also have to use %s instead of %c in printf in main function.

Sign up to request clarification or add additional context in comments.

Comments

1

Your function has a return type of char and you are trying to return const char* (since string literals are immutable). The error means that you are trying to convert a pointer into a number, which is not what you want.

Just change your function to return const char* and it will be correct.

Comments

1
char and Char* are not same thing. char as a data stores just one character like char x = 'c'; char* on other hand is pointer to the base address of character type array. like char* msg ="Hello World"; Your return type should be char* because you are returning a char* type variable which contains the base address of string. 

Comments

0

You show your return of the function as a char but your return statement returns a pointer to a char which isn't the same thing.

Comments

0

Try this instead:

const char *BoolToString(bool prime, bool even) { if (prime && even) { const char* result = "is even and prime"; return result; } else if (prime && !even) { const char* result = "is odd and prime"; return result; } else if (!prime && even) { const char* result = "is even and not prime"; return result; } else if (!prime && !even) { const char* result = "is odd and prime"; return result; } else { const char* result = "error"; return result; } } 

1 Comment

Shouldn't the type of result be const char*?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.