![]() |
| | #1 |
| Registered User Join Date: Sep 2005
Posts: 92
| Pointers Question 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[]) Code: float *It1, *It2, *It3; It1 = I1[]; It2 = I2[]; It3 = I3[]; 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. |
| HAssan is offline | |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,295
| Code: float *It1, *It2, *It3; It1 = I1[]; It2 = I2[]; It3 = I3[]; 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("%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;
}
Anyway, my post gives you all the details (yes they're all in there somewhere). You just have to fish! Last edited by zacs7; 09-08-2008 at 06:28 AM. |
| zacs7 is offline | |
| | #3 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| Code: float Current_Dependant_Force(float I1[], float I2[], float I3[]) http://cboard.cprogramming.com/showp...6&postcount=10 |
| robwhit is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array pointers question? | ben2000 | C Programming | 4 | 07-26-2007 01:31 AM |
| A question on Pointers & Structs | FJ8II | C++ Programming | 4 | 05-28-2007 10:56 PM |
| simple pointers question | euphie | C Programming | 4 | 05-25-2006 01:51 AM |
| Very stupid question involving pointers | 7smurfs | C Programming | 6 | 03-21-2005 06:15 PM |
| Quick Question Regarding Pointers | charash | C++ Programming | 4 | 05-04-2002 11:04 AM |