# Thread: Pointers Question

1. ## Pointers Question

Hello

I bumped into this problem and I am unsure how to solve this. I have a function that accepts 3 arrays as its function parameter. Each array has 360 elements.

Code:
`float Current_Dependant_Force(float I1[], float I2[], float I3[])`
and I have declared 3 pointers to the address of the first element of the array

Code:
```float *It1, *It2, *It3;

It1 = I1[];
It2 = I2[];
It3 = I3[];```
My question is that how do I get the pointers to dereference whats inside the array and increment it to use the next value.

Something like this

A= It1[0]*It2[0] It1[360]*It2[360] (I want to use pointers to perform this operation)

I would be very greatful if anyone can help.

2. Code:
```float *It1, *It2, *It3;

It1 = I1[];
It2 = I2[];
It3 = I3[];```
It1 holds the address of the first element of I1, thus (It1 + 1) is the address of the second, etc. Therefore, *It1 = the value of the first element, *(It1 + 1) the value of the second element. Simple pointer arithmetic.

It makes more sense, and is more clear just to use the pointer notation rather than the array notation to begin with even though they mean the same thing (most of the time). That means you can use I1 like you use It1, making It1 useless.

You should also pass the size of array in, rather than using a magic '360' value.
Code:
```#include <stdio.h>

float something(const float * one, const float * two, const float * three, size_t n);

int main(void)
{
float x[] = {5.0f, 10.0f, 3.0f};
printf("&#37;d, %f\n", sizeof(x) / sizeof(*x), something(x, x, x, sizeof(x) / sizeof(*x)));
return 0;
}

float something(const float * one, const float * two, const float * three, size_t n)
{
size_t i = 0;
float acc = 0.0f;

/* make sure you check that one, two and three are not NULL!
* you can't dereference NULL pointers */

/* keep us in bound, going from [0,n) */
for(i = 0; i < n; ++i)
{
/* you could put this all on one line, but it's easier to read this way */
acc += *one + *two + *three;

++one;      /* move the first array spot one right */
++two;
++three;
}
return acc;
}```
Notice how I made the arguments constant, so I know the value of the array doesn't change.

Anyway, my post gives you all the details (yes they're all in there somewhere). You just have to fish!

3. Code:
`float Current_Dependant_Force(float I1[], float I2[], float I3[])`
Note that those are all pointers, not arrays. Any array parameter is really a pointer.

http://cboard.cprogramming.com/showp...6&postcount=10

Popular pages Recent additions