Originally Posted by
uural4792
I'm creating a program that rates bowlers based on their average score and assigns a star value for their performance(ex : *** means their average is between 170 and 199) and I need to create a function that allows me to do that.
I successfully created a function that allows me to calculate the averages but the thing I'm having trouble doing is making the function output a string. As of now I have the following :
...
Am I defining these functions correctly? Should they be str findstars for example? Or char* findstars?
The error I'm getting in my compiler is that my function returns an address of local variables.
MAX is defined before everything as 100.
C does not know how to return an array from a function. Strings are just arrays of characters. So that ain't gonna happen. (Yeah I know... WTF??? ... but that's the way it is.)
Here's an example to demonstrate why you should not return arrays from functions...
Code:
#include <stdio.h>
int* MyFunction(int a, int b, int c)
{ static int array[3];
array[0] = a;
array[1] = b;
array[2] = c;
return array; } // return a pointer.
int main (void)
{ int *a1, *a2; // int pointers
printf("calling a1 = MyFunction(10,20,30);\t");
a1 = MyFunction(10,20,30);
printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
printf("calling a2 = MyFunction(100,200,300);\t");
a2 = MyFunction(100,200,300);
printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
printf("\nLooks good, except...\t");
printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
getchar();
return 0; }
Compile this and run it... Try it both with and without the static keyword.... you'll get the idea.
If you want to modify an array from within a function, pass in a pointer to the array and work on the pointers.