简体   繁体   中英

Why return pointer cannot be assigned to a pointer, array actually is not a pointer?

#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 ?

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.

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" );

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];
}
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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM