Thread: function with array arguement

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    function with array arguement

    hey, i have written this code just to transfer a few number of elements from one array to another two arrays, but I had some problem calling the function. Below is the code:
    Code:
    #include<stdio.h>
    void copy1(float a[],float b[],int c);
    void copy2(float *d,float *c,int a);
    int main(void)
    {
        int a;
        float *b;
        static float rain[12]=
        {10.2,8.1,6.8,4.2,1.5,2.6,2.6,6.4,5.8,4.5,1.1,5.9};
        static float copy1[12]=
        {0,0,0,0,0,0,0,0,0,0,0,0};
        static float copy2[12]=
         {0,0,0,0,0,0,0,0,0,0,0,0};
       copy1(rain,copy1,6);
       copy2(rain,copy2,5);
       b=copy1;
       for (a=0;a<12;a++)
       
         printf("%4.1f",*(b+a));   
       
       
        system("pause");
        return 0;
    }
    void copy1(float a[],float b[],int c)
    {
         int d;
         for (d=0;d<c;d++)
           b[d]=a[d];
         
    }
    void copy2(float *d,float *c,int a)
    {
         int b;
         for (b=0;b<a;b++)
          (c+b)=*(d+b);
    }
    after I called the copy1 and copy2 function, there was a error saying"called object is not a function", how can this happen? and the last assignment (c+b)=*(d+b); is invalid, why?

  2. #2
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    (c+b)=*(d+b);

    What is this! you can't do that can you? you can only asign the output of a function or such to a single variable! it doesn't make sense to me!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    1. the function names should be different to the array names. that is change the function names
    2. change

    Code:
    int b;
    for (b=0;b<a;b++)
          (c+b)=*(d+b);
    to

    Code:
    int b;
    for (b=0;b<a;b++)
          *(c+b)=*(d+b);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void copy2(float *d,float *c,int a)
    {
         int b;
         for (b=0;b<a;b++)
          (c+b)=*(d+b);
    }
    Quote Originally Posted by bobthebullet990
    (c+b)=*(d+b);

    What is this! you can't do that can you?
    Yes and no. This is simple pointer math. Given an address, adding an integer value to it will give you the address that value further along. Say we have an array, which has five values. Say the address of the first entry is at memory address 0x01, and the second is at 0x02, and so on. If we take the first address, 0x01, and add 3 to it, it will give us the element 3 places further along. Make sense?

    Ok, now what about the size? How far is "one further along"? Well, when you add a value to the pointer, you're really adding the "size of the object being pointed to * the value" in actual bytes. This way, if we have an integer as our base type in the above example, when we add 3 to it, it moves along three times the size of an integer. If we had an array of characters, or floats, or structures, we still move the right amount down the line.

    However, that being said, this example is flawed, because they should be dereferencing the first address also, or, not dereferencing the second. Dereference both, or neither, depending one how you're "copying".


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by quzah
    [/code]
    However, that being said, this example is flawed, because they should be dereferencing the first address also, or, not dereferencing the second. Dereference both, or neither, depending one how you're "copying".
    Quzah.
    If you do not dereference the (c+b) you will not get a l-value... so you cannot assign to it even if the right-side pointer will not be dereferenced...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void wrong( char *a, char *b, size_t x )
    {
        size_t c;
        for( c = 0; c < x; c++ )
        {
            (a + c) = (b + c);
        }
    }
    I'll blame lack of my morning cup o' joe. You're right. Thinking about it a bit more, a pointer plus a value gives a value. Values cannot be assigned to each other.
    Code:
    7 = 3;
    So then...
    Code:
    void right( char *a, char *b, size_t x )
    {
        size_t c;
        for( c = 0; c < x; c++ )
        {
            *(a + c) = *(b + c);
        }
    }
    But dereferencing both works. Dereferencing one, as stated, will not work.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM