# Thread: function with array arguement

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

3. 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. Code:
```void copy2(float *d,float *c,int a)
{
int b;
for (b=0;b<a;b++)
(c+b)=*(d+b);
}```
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.

5. 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...

6. 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.