-
char array
Hello everyone. I am writing a simple function that returns a char array. Here is the code that I have so far. Can anyone help me fix the errors I am having working with the char array? Thanks.
char[] getLetterGrade(float average)
{
char[] letterGrade;
//this code checks the average passed to it and returns the proper grade
if(average>=93) letterGrade='A';
else
if(average>=87) letterGrade='AB';
else
if(average>=83) letterGrade='B';
else
if(average>=77) letterGrade='CB';
else
if(average>=73) letterGrade='C';
else
if(average>=67) letterGrade='CD';
else
if(average>=60) letterGrade='D';
else
if(average<60) letterGrade='F';
return letterGrade;
}
-
You have a lot of newbie string errors in there. For passing arrays as well as for using strings.
I suggest you read up on how strings work in C.
-
Arrays are not pass-by-result, therefore your header should be
void getLetterGrade(float average, char letterGrade[])
That should help a bit.
-
-
You also cannot return an array allocated within a function. This is very unsafe. It should be ever allocated dynamically inside a function or copied to an outside target.