functions and pointers

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sPtr(int *numbers, int size, int *pMinimum, int *pMaximum, double *pAverage)
{
	double sum = 0;

	pMinimum = pMaximum = numbers;
	for (int i = 0; i < size; i++)
	{
		if (numbers < pMinimum)
			pMinimum = numbers;
		else if (numbers > pMaximum)
			pMaximum = numbers;
		sum += *numbers;
	}
	pAverage = (sum / size);
}

Hello guys!
So I am messing around with some pointers but I cant seem to find a solution for one of my functions. basically the last line where I have to assign the pointer to the average it throws an error. I am wondering how can I solve this problem so I can pass the average back as a pointer. Thank you for any help.
pAverage is a pointer, when you use it without asterisk (*) pAverage you're accessing memory address of the pointer, not value pointed by pointer.

to access and modify the value pointed by the pointer use asterisk:
*pAverage = (sum / size);

in your code:
pAverage = (sum / size);

you are modifying pointer address and it become invalid.

you might need to review other pointers as well to make sure you access/assign values and not memory addresses.
Topic archived. No new replies allowed.