Pointers Question

This is a discussion on Pointers Question within the C Programming forums, part of the General Programming Boards category; Hello I bumped into this problem and I am unsure how to solve this. I have a function that accepts ...

  1. #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.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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!
    Last edited by zacs7; 09-08-2008 at 06:28 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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.

    Passing 2 dimensional array to a function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

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