I was trying to calculate an average, and return the value using pointers in the function. Instead, it keeps returning the memory address. If anyone could give me some insight, I would greatly appreciate it.
double average(double* scores, int numScores)
{
double *avScores; // avScores is a pointer, but doesn't point to anything (bad pointer)
for (*scores; *scores < numScores; *scores++) // this just keeps incrementing the first
// score in the array until it is >= the number of scores. It does
// does not look at any scores beyond the first
{
// avScores is a bad pointer. trying to dereference it = BAD BAD BAD
// memory corruption, possible program crash
*avScores = *scores/numScores;
}
// again... avScores is a bad pointer. can't dereference it!
return *avScores;
}
There is no reason for avScores to be a pointer here. There's nothing for it to point to. You just want a normal double here.
Also, you can treat 'scores' like a normal array in this function.
EDIT: Actually I might be wrong on your for loop.... I can never remember if *foo++ is (*foo)++ or *(foo++). Either way, it's not doing what you think it does.
Thank you for your reply. I just tried that solution, and I keep getting errors. Integers and doubles are in the function, so I keep getting an incompatibility error.
I just changed some things around with your advice as well as Vlad's, and I still can't make it work... Unfortunately, I can't use any array notation in the function itself. I have to use pointer notation.