1

I am trying to reference an array by using pointers

double a[5];
double*& b = a; //doesn't compile
double*& b = &a[0]; //doesn't compile either
double*& b = &static_cast<double*>(a); //nope

Is there any way to make it so that b is binded to the address of the first element of the array?

thanks!

lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • Why not just use `double* b = a;`? – Joel Aug 02 '13 at 13:30
  • `double *b = a;` will point `b` to the first element of `a` by default... See [here](http://stackoverflow.com/questions/1106957/pass-array-by-reference-in-c) for a further explanation... – NREZ Aug 02 '13 at 13:30
  • Or, if you don't want it modifiable, `double* const b = a;` – Joel Aug 02 '13 at 13:30
  • this isn't possible, I'll add context in a few mins – lezebulon Aug 02 '13 at 13:35
  • ok... after trying to type out the context of my question I realized that what I wanted to do makes no sense... What I wanted was to bind to the pointer of the first element, then modify this pointer to point to a new location so that everytime I tried to read the original array, it would instead read to the new location. but it really makes no sense as I have a normal array and not a pointer to an array in the first place... thanks for the answers! – lezebulon Aug 02 '13 at 13:43
  • Then is sounds like `double* b = a;` would do exactly what you want. – juanchopanza Aug 02 '13 at 13:47
  • An array is **not** a pointer – David Rodríguez - dribeas Aug 02 '13 at 13:51

4 Answers4

2

To reference an array by using pointers, you don't need anything special:

double* b = a;

If you want a pointer to the entire array, rather than to just the first element, it is:

double (*b)[5] = &a;

, but this very unusual, and will enormously confuse any reader.

If you want a reference to the entire array (no pointer):

double (&b)[5] = a;

This is often used as a function parameter, but I've never seen it used elsewhere.

If you want a reference to a pointer designating the first element, it has to be const, because the result of a conversion (including the array to pointer conversion) is an rvalue, and cannot be bound to a non-const reference:

double *const (&b) = a;

And if you really need a non-const reference to a pointer, you'll have to introduce a pointer variable:

double* pa = a;
double* (&b) = pa;
James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

In this program a pointer an array is used:

//#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
    int a[5] = {1,2,3,4,5};
    int b[5] = {6,7,8,9,10};
    int (*pa[4])[5] = {&a,&b,&a,&b};
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 5; j++)
            cout<< (*pa[i])[j] << "
        cout<< "\n";
    }
    //_getch();
    cin.get();

}

In some compilers comparison of &a and a is allowed. For example this code can be compiled in (old) Borland compilers:

//#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
    int a[20];
    cout<< (a == &a);
    //_getch();
    cin.get();
}

and the output is 1. But in previous program instead of

int (*pa[4])[5] = {&a,&b,&a,&b};

one cannot write

int (*pa[4])[5] = {a,b,a,b};
Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
0

As others have remarked, you don't need a reference-to-a-pointer, just use a pointer-to-double.

 double *b = a;

The reason why you can't assign a to a reference-to-a-pointer is because the silent conversion from array-of-double to pointer-to-double results in an rvalue.

If you have to use a reference to pointer, you should make it a reference to const pointer, const references can bind to rvalues:

double a[5];
double *  const &b = a;
dhavenith
  • 2,028
  • 13
  • 14
0

You can get a pointer to the first element:

double * b = a;

or a const reference to an unnamed pointer to it:

double * const & b = a;

or a reference to the array:

double (&b)[5] = a;

but there's no way get a non-const reference to a pointer without a pointer variable to refer to.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644