how can i write a function that returns a string in c?
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?
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;
}
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");
}
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?
see this answer
As other said add an else with some return value ... And tell us exactly what the error is, please !
my2c
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
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.
If you want to return a string literal like return "blah"
, return type should be const char*
.
精彩评论