# Thread: Function w/ pointer errors

1. ## Function w/ pointer errors

When I call the following function:
Code:
```void avg_sum (float a [10], int n=9 , float *avg, float *sum) {

int i;
float s, v:
sum = &s;
avg = &v;
for (i = 0; i < n; i++) {

s += a [i];
}
avg = sum/n;
}```
I get errors. I know something is wrong, but I cant put my finger on it.

2. what are the errors and how are you calling the function?? I think you mean astericks as well when you assign those floats. Also, you assign the value of n to nine in the paramater list, this is a c++ property, not c. Also, look at how you are passing that array, use pointers instead.

3. A pointer is a variable that contains an address (of some other variable). You have to use the asterisk (*) operator to read/write to the data at that address.

Code:
```void avg_sum (
float a [10], // don't need the size here, it's in 'n'
int n=9 , // C doesn't allow default parameters
float *avg,
float *sum) {

int i;
float s, v: // don't need these, use the pointers
sum = &s;
avg = &v;
for (i = 0; i < n; i++) {

s += a [i];
}
avg = sum/n; // use * appropriately
}```

This is all you have to do:

Code:
```void avg_sum (float a[], int n, float *avg, float *sum) {

(*sum) = 0;

int i;

for (i = 0; i < n; i++) {

(*sum) += a [i];
}
(*avg) = (*sum)/n;
}

int main()
{
const int size = 6;
float data[size] = {1.4, 3.0, 7.32, 2.5, 3.14, 14.1};
float sum, average;

avg_sum(data, size, &sum, &average);

printf(
"# elements: %d\n"
"sum total: %g\n"
"avgerage: %g\n",
size, sum, average);

return 0;
}```

4. So when do you not use the * when using a pointer. I think this whole pointer concept is going over my head.

5. You use the * when you want to get at the contents of the variable that the pointer is pointing to.

for example

Code:
```#include <stdio.h>

int main(void)
{

int i = 7;
int *p;

p = &i; /* make p point to i */
/* p holds the address of the variable i */

printf("The value of i is %d\n", *p);
return 0;
}```
You see how that works? p points to i, but if you want to see the contents of i, you use the * ( to dereference). I know you asked when not to use the *, but if you know when to use it, I suppose you know when not to use it also.

A good tutorial on pointers and arrays can be found here

6. in your pointer declaration, you didnt use the *, but above, the * is used. I guess that is where I am confused.

7. I am not following you.

8. you wrote:

p = &i;

above, it is a function. in the previous post to yours.

So the reason he put all the * in there, like for the *sum things, is because the fucntion is later called thats where you use the &?

I dunno. I feel lost too.

9. * is a dereferencing operator - think of it as 'the contents of' operator - when you use it, it is not the memory address, but the contents of the variable that the pointer is pointing to that you are dealing with. If you don't use it, you are dealing with the address of the variable that the pointer points to.

Have a read of the tutorial I posted above. Look carefully at Sebastiani's code, and try and see what he has going on there.

10. I didn't even see the link. Ill take a look. thanks for the help.

11. Okay, these principles work for all variables (even pointers, yes, you can have pointers to pointers!). First of all, when you see a declaration:

float * p;

The asterisk isn't doing anything there except telling the compiler you want a pointer to a float.

Now when you want the address of a variable, you use the amperstand, of course:

float * p1 = NULL;
float * p2 = NULL;
float num = 10;

p1 = &num;
p2 = p1;
*p2 = 30; // 'num' now contains the value of 30

Notice that you don't use the dereference operator to assign a pointer to a pointer - they are the same type of variable, that's why.

OK, pretend we could see both the address (A) and the contents (C) of the variables. The above might look like:

float * p1 = NULL; // A: 1234 - C: 0
float * p2 = NULL; // A: 9658 - C: 0
float num = 10; // A: 5454 - C: 10

p1 = &num; // p1 = // A: 1234 - C: 5454
p2 = p1; // p2 = // A: 9658 - C: 5454

*p2 = 30;

The last line loads the contents of p2 (the address of num), and then assigns content 30 to it (num).

Sorry if I don't explain very well, but that's basically it.