Elise
Elise

Reputation: 131

function returning a char (beginner)

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.)

Upvotes: 0

Views: 170

Answers (5)

Arvind Krmar
Arvind Krmar

Reputation: 2622

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.

Upvotes: 1

Magisch
Magisch

Reputation: 7352

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;                                                                                                                                       
  }                                                                            

}

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

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.

Upvotes: 6

Rob
Rob

Reputation: 15168

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.

Upvotes: 0

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31203

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.

Upvotes: 1

Related Questions