-1

[edited] I got my code working well now. But can someone explain why a is passed to b by b = a and not b = &a ? I thought I had to get b to point to the address to a using &. Thanks.

Code:

#include <iostream>
using namespace std;
void myf(double a[]);

int main()
{

double a[]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};


myf(a);


system("pause");
return 1;   
     }

void myf(double a[])
{
    double * b;
    b = a;


        cout << "This is a : " << a[0] << " " << a[1]  <<" " << a[2] <<" " << a[3] <<" " << a[4] <<" " << a[5] <<" " << a[6] <<" " << a[7] << endl;
        cout << "This is b : " << b[0] <<" " << b[1] <<" " << b[2] <<" " << b[3] << " " <<b[4] <<" " << b[5] <<" " << b[6] <<" " << b[7] << endl;


}
Alfie brown
  • 87
  • 2
  • 2
  • 9
  • can you post what kind of error you get? – Kick Buttowski Oct 15 '15 at 21:43
  • 1
    Your function is declared to take as parameters pointers to doubles, but you attempt to call the function with the pointer to an array. Just like when calling a sushi delivery to fix your broadband, you'll only be met with confusion. – Kerrek SB Oct 15 '15 at 21:43
  • change `myf(&a,&b); ` to `myf(a,b);`. Additional notes... `*b=*a` would only assign the first elem of a to b (will not copy the array 'a' to array 'b'). Same follows for `cout` (only the first elem will be printed) – knightrider Oct 15 '15 at 21:45
  • @KerrekSB ok yes. What you said make sense but can you show the correction? should I change the first argument to array : void myf(double a[], double *b); – Alfie brown Oct 15 '15 at 21:47
  • @Alfiebrown In function arguments, an array is the same as a pointer. `double a[]` would therefore accept a pointer. Even `double a[any_number]` would accept a pointer. In fact, you can't pass arrays to functions as arrays. They will first have to [decay to pointers](http://stackoverflow.com/questions/1461432/what-is-array-decaying?lq=1). – Emil Laine Oct 15 '15 at 21:58
  • Referencing the array as `a` is the address of the array. – lit Oct 15 '15 at 23:37
  • @Alfiebrown: There are three further problems with your suggestion: 1) You cannot pass arrays by value. 2) Even if you could, you would be changing the local copy, not the original array. 3) You cannot assign arrays. – Kerrek SB Oct 16 '15 at 00:21

4 Answers4

1

Since b is an array, &b is a pointer to an array, i.e. double (*)[8]. However your function wants a pointer to a double, not a pointer to a double[8].

To solve this, you can use array-to-pointer decay:

myf(a, b);

The compiler sees that myf needs a double* and you're passing a double array. It will then implicitly convert the array to a pointer to the first element in that array.

Btw your code has undefined behavior because b hasn't been initialized before it's first use.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0
void myf(double *a, double *b)

Is expecting double pointers, or (double*).

myf(&a,&b);

Is passing in double pointers-to-pointers, or (double**).

That's why it's not compiling.

With respect to the functionality, you need to clarify: do you want array b to point to array a? Or do you want to copy the contents of array a into array b? Those are not the same thing, and the semantics are very different.

EDIT:

In response to the OP's comment, what needs to happen is that myf needs to be redeclared:

void myf(double *a, double **b)
{

    *b=a;
    //Other debug stuff
}

OR:

void myf(double *a, double *&b)
{

    b=a;
    //Other debug stuff
}

Both are functionally equivalent, but as a matter of good practice, it's generally expected that you use the first form.

However, there's some other stuff that needs to be taken into account when rewriting the code. For example:

int main()
{

    double a[]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};
    double b[8];

    myf(a,&b);


    system("pause");
    return 1;   
}

This will yield bad, probably seg-faulty errors. Even if your compiler lets it happen (and it shouldn't), you probably don't want to write it like that. Instead, write like this:

int main()
{

    double a[]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};
    double * b;

    myf(a,&b);

    system("pause");
    return 1;   
}
Xirema
  • 19,889
  • 4
  • 32
  • 68
0

Pointers take a while to get straight, don't worry.

In myf your pointers point to a single double so the assignment statement can only copy the contents of where the 'a' is pointing to the place where the 'b' is pointing.

When I was starting out I found it helpful to draw out memory as blocks on graph papers. Number each 'location' on the graph paper. Now what are pointers and variable names in the paper world?

john elemans
  • 2,578
  • 2
  • 15
  • 26
0
#include <iostream>
using namespace std;
void myf(double *a, double *b, int len);

int main()
{

    double a[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8};
    double b[8];

    myf(a, b, 8);


    system("pause");
    return 1;
}

void myf(double *a, double *b, int len)
{

    for(int i = 0; i < len; i++)
        b[i] = a[i];

    cout << "This is a : ";
    for(int i = 0; i < len; i++)
        cout << *a;
    cout << endl;

    cout << "This is b : ";
    for(int i = 0; i < len; i++)
        cout << *b;
    cout << endl;


}
knightrider
  • 2,063
  • 1
  • 16
  • 29
  • Recommend explaining what you did and why you did it. Code-only answers often don't teach much more than cut and paste. – user4581301 Oct 15 '15 at 22:28