0
#include<stdio.h>
double *array_transmission(double *a,double *b,int c);
int main(void)
{    int number=8;
     int i;
    double easy_array[]={1,2,3,4,5,56,7,8};
    double copy1[8];
    copy1=array_transmission(easy_array,copy1,number);
    for(i=0;i<number;i++)
       printf("%d\t",copy1[i]);
    return 0;
}
double *array_transmission(double *a,double *b,int c)
{
    int i;
    b=a;
    return b;
}

I just want to copy an array. But the returned pointer value is error. I will at loss, what to do ?

Manjunath N
  • 1,365
  • 11
  • 22
qiabigin
  • 45
  • 7

4 Answers4

4

In your code,

copy1=array_transmission(easy_array,copy1,number);

What you wanted to do is to take the return value from the function array_transmission() and put the value into copy1, which represents the base address of an array.

double copy1[8]; is a automatic stack-allocated array. You're not supposed to change the base address of that array.

And yes, array and pointers are different. It's better you treat them that way.


EDIT:

Also, as mentioned by haccks, in your array_transmission() function what you're returning is an automatic local [to that function] pointer variable . You may not return a pointer to automatic local variable from a function. The scope of an auto local variable is restricted to that function only. Once the function has finished execution, there is no existence of that variable on stack and hence, the usage of the returned pointer in the caller function is also invalid.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Answer can be improved further by adding that *a pointer to automatic local variable can not be return.....*. – haccks Dec 29 '14 at 08:23
3

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" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Maybe I was not clear enough expressed in the question, if it is the reason I am very sorry. My question is to be printed in the main function, rather than a simple subroutine in print. – qiabigin Dec 29 '14 at 09:19
  • @qiabigin I wrote in my post that you may not assign the return value of the function that is a pointer to an array name. You have to define in this case a pointer in main and assign the return value of the function to this pointer. For example double *p = array_transform( /*...*/ ); I wonder it seems that asterisk is not displayed in the comment. I defined p as a pointer. – Vlad from Moscow Dec 29 '14 at 09:24
  • from MoscowThank you very much for your help, by you I have solved my problem. – qiabigin Dec 29 '14 at 09:43
0

When you call the function double *array_transmission(double *a,double *b,int c) the parameter b is a local copy of what you pass on the call copy1=array_transmission(easy_array,copy1,number); when you exit the function, this copy is deleted so you return a pointer to nothing.

if you want that copy1 will point to easy_array (and not to a copy of easy_array) and can simply do

double* copy1; 
copy1 = easy_array;

if you want a new copy of easy_array you need to do this:

for (i=0;i<8;i++) {
   copy1[i] = easy_array[i];
}
Ariel M
  • 175
  • 2
  • 10
0
double *array_transmission(double *a,double *b,int c)
{
    int i;
    b=a;
    return b;
}
  1. The pointers a and b are local variables. Changing their values will not affect anything outside the function.
  2. You need to copy the memory that the pointers point to rather than the pointers themselves: memcpy (b, a, c * sizeof *b).
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42