C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-08-2008, 06:02 AM   #1
Registered User
 
Join Date: Sep 2005
Posts: 92
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.
HAssan is offline   Reply With Quote
Old 09-08-2008, 06:21 AM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
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("%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!
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 09-08-2008 at 06:28 AM.
zacs7 is offline   Reply With Quote
Old 09-08-2008, 10:17 AM   #3
Registered User
 
Join Date: Oct 2001
Posts: 2,110
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
robwhit is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:42 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22