If you want to copy one array into another array you have to copy them element by element. The function could look like
double * array_transmission( cosnt double *src, double *dst, size_t n )
{
while ( n-- ) *dst++ = *src++;
return dst;
}
As for your function implementation
double *array_transmission(double *a,double *b,int c)
{
int i;
b=a;
return b;
}
then you indeed may write
b=a;
but the effect will not what you are expecting.
a
and b
are local variables of the function. And though inside the function b is set to the value of a but this does not do coping of the arrays. Simply now a and b point to the same object that is the first element of the array pointed to by a. After exiting the function local variables a and b will be destroyed but elements of the arrays were not copied.
In the function you deal with pointers. a and b are pointers to first elements of the corresponding arrays. You may assign one pointer to another. But arrays are not pointers. They have no the copy assignment operator. For example you may not write the following way
double a[] = { 1.1, 2.2, 3.3 };
double b[] = { 4.4, 5.5, 6.6 };
b = a;
The compiler will issue an error.
Compare this code snippet with the following
double a[] = { 1.1, 2.2, 3.3 };
double b[] = { 4.4, 5.5, 6.6 };
double *pa = a;
double *pb = b;
pb = pa;
In this case you assign pointer pa to pointer pb (the same way as you did in your function). But arrays themselves were not copied. Simply now the both pointers point to the first element of array a.
Also take into account that you may not write
double a[] = { 1.1, 2.2, 3.3 };
double b[] = { 4.4, 5.5, 6.6 };
b = array_transmission( a, b, 3 );
because again arrays are not pointers and have no the copy assignment operator (it is a C++ term). In fact there is no need to use the assignment because inside the function the elements were already copied from one array into another (provided that you wrote the function as I showed).
You can write simply
double a[] = { 1.1, 2.2, 3.3 };
double b[] = { 4.4, 5.5, 6.6 };
size_t i;
array_transmission( a, b, 3 );
for ( i = 0; i < 3; i++ ) printf( " %f", b[i] );
printf( "\n" );
When you can ask why do the function in this case have return type double *
? it is very useful. Consider for example a situation when you need to copy two arrays into a third array one ofter another. Then you could write for example
double a[] = { 1.1, 2.2, 3.3 };
double b[] = { 4.4, 5.5, 6.6 };
double c[6];
size_t i;
array_transmission( b, array_transmission( a, c, 3 ), 3 );
for ( i = 0; i < 6; i++ ) printf( " %f", c[i] );
printf( "\n" );