3

When I try calling my function using printf(" %s",course_comment(1.0) );, the program crashes. This is my function:

char *course_comment(float b) 
{ 
   if(b < 2.0) 
     return("Retake"); 
}

Why does it crash? How can I fix it?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Wasswa Samuel
  • 2,139
  • 3
  • 30
  • 54
  • If that's really the whole function, you're not returning anything if `b >= 2.0` (that's not your particular problem here, but it will be a problem at some point) – Michael Mrozek Feb 10 '11 at 15:11
  • Works fine for me if I add a missing parenthesis. What goes wrong? – Fred Foo Feb 10 '11 at 15:11
  • "it doesn't work" is quite broad when talking about C. Be more specific :) – Aiden Bell Feb 10 '11 at 15:33
  • possible duplicate of [how to return a string in my c code .](http://stackoverflow.com/questions/4929329/how-to-return-a-string-in-my-c-code) – Aiden Bell Feb 10 '11 at 15:36
  • http://stackoverflow.com/questions/4929329/how-to-return-a-string-in-my-c-code possible dupe, and there will be tons more I recon – Aiden Bell Feb 10 '11 at 15:36
  • doesn't work means it compiles and runs without a glitch but unexpectedly crashes. – Wasswa Samuel Feb 10 '11 at 16:23
  • @Wasswa Show your complete code, not just these snippets. And you say it crashes, but you haven't provided the error message. Compile your code with debug symbols, and run the debugger when it crashes to establish that it died in that printf. – Jim Balter Feb 11 '11 at 06:09

7 Answers7

4

If your strings are constants and there is no intention to modify the result, working with string literals is the best choice, e.g.:

#include <stdio.h>

static const char RETAKE_STR[] = "Retake";
static const char DONT_RETAKE_STR[] = "Don't retake";

const char *
course_comment (float b)
{
  return b < 2.0 ? RETAKE_STR : DONT_RETAKE_STR;
}

int main()
{
  printf ("%s or... %s?\n",
      course_comment (1.0), 
      course_comment (3.0));
  return 0;
}

Otherwise, you can use strdup to clone the string (and don't forget to free it):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *
course_comment (float b)
{
  char result[256];

  if (b < 2.0)
    {
      snprintf (result, sizeof (result), "Retake %f", b);
    }
  else
    {
      snprintf (result, sizeof (result), "Do not retake %f", b);
    }
  return strdup (result);
}

int main()
{
  char *comment;

  comment = course_comment (1.0);
  printf ("Result: %s\n", comment);
  free (comment); // Don't forget to free the memory!

  comment = course_comment (3.0);
  printf ("Result: %s\n", comment);
  free (comment); // Don't forget to free the memory!

  return 0;
}
Pacerier
  • 86,231
  • 106
  • 366
  • 634
3

Depending on the order / structure of your program when 'course_comment' is first called - it maybe be undeclared & C will default its return type to an 'int'. Check for compiler warnings when you err.. compile.

Also make sure you understand about function prototypes, when & where they should be used (everywhere basically). I think the 'f' missing on the 1.0 means the argument will be auto cast to an int.

This works - not that I would ever do this:

#include <stdio.h>

const char *course_comment(float b); // <- fn prototype


int main(int argc, char *argv[]) {

    printf(" %s",course_comment(1.0f));


}


const char *course_comment(float b) 
{ 
   if(b < 2.0) 
     return("Retake"); 
}
David Victor
  • 830
  • 9
  • 29
2

You should probably return a literal as const char * as they cannot be modified.

what does your function return if b is not less than 2.0? what do you think would happen if you tried to use the return value? Is your exact code what is crashing?

CashCow
  • 30,981
  • 5
  • 61
  • 92
1

see this answer

As other said add an else with some return value ... And tell us exactly what the error is, please !

my2c

Community
  • 1
  • 1
neuro
  • 14,948
  • 3
  • 36
  • 59
1

because your getting a NULL-pointer since 1.0 doesn't return anything.

Not your function crashes, printf crashes with:

printf(" %s", NULL);

Rule Of Thumb:

  • always have a defined return
  • gcc -Wall shows you all warnings
boecko
  • 2,195
  • 1
  • 12
  • 13
1

Return a pointer like that isn't particularly pretty. The least evil thing you can do is something like this:

main.c

#include "some_other_file.h"

int main()
{
  printf(" %s", course_comment(1.0f) );
  return 0;
}

some_other_file.h

#ifndef YADA_YADA_H
#define YADA_YADA_H 

const char* course_comment(float b);

#endif

some_other_file.c

static const char COMMENT_RETAKE [] = "Retake";



const char* course_comment(float b) 
{ 
  const char* result;


  if(b < 2.0f)
  {
    result = COMMENT_RETAKE;
  }
  else
  {
    result = ""; /* empty string */
  }

  return result;
}

Please note that you should use 1.0f notation when dealing with floats, and 1.0 notation when dealing with doubles. Otherwise the compiler will do silent promotions of your variables to double, and the code will turn slower.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

If you want to return a string literal like return "blah", return type should be const char*.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33