I was wondering If someone could let me know the best way to return a sprintf string.
So (I think) I understand why this isn't working at all, it's because once the function is run everything on the stack is destroyed so buffer is returning noting.
I have a couple of similar functions like the one below and I'm out of ideas on how I should go about returning the sprintf string. Should I malloc the buffer for every function?
const char* gradeCheck(double mark) {
char buffer [100];
if (mark > MAX_MARK || mark < MIN_MARK) {
sprintf(buffer, "INVALID TYPE\n");
return buffer;
} else if (mark >= PASSING_MARK && mark <= MAX_MARK) {
sprintf(buffer, "Student achieved %.2lf\n", mark);
return buffer;
} else {
sprintf(buffer, "Student achieved %.2lf which means they failed ;(\n", mark);
return buffer;
}
}