5

http://www.neilstuff.com/guide_to_cpp/notes/Multi%20Dimension%20Arrays%20and%20Pointer%20Pointers.htm

According to this site, I should be able to use the following code:

double stuff[3][3];
double **p_stuff;
p_stuff = stuff;

But I get a complaint that the conversion is not allowed by assignment.

Am I doing something wrong?

I have an extern "C" type function that I want to pass this double stuff[3][3] to. So I think i need to make it a pointer, right?

6
  • 3
    No the note is wrong. double[3][3] cannot be decayed to double** (it decays to (double*)[3]).
    – kennytm
    Mar 16, 2011 at 17:21
  • Try p_stuff = new double *stuff[3] after the double **p_stuff
    – George
    Mar 16, 2011 at 17:23
  • What is the signature of this C function?
    – Erik
    Mar 16, 2011 at 17:26
  • Edited question to reflect my intent..getting this double stuff[3][3] to a C function.
    – Derek
    Mar 16, 2011 at 17:29
  • I am writing the C function as well...so I can make the signature whatever I want. Right now I had it as (....., double **p_stuff, ...)
    – Derek
    Mar 16, 2011 at 17:29

5 Answers 5

15

Regarding the edit: to pass this double stuff[3][3] to a C function, you could

1) pass a pointer to the whole 2D array:

void dostuff(double (*a)[3][3])
{
// access them as (*a)[0][0] .. (*a)[2][2]
}
int main()
{
    double stuff[3][3];
    double (*p_stuff)[3][3] = &stuff;
    dostuff(p_stuff);
}

2) pass a pointer to the first 1D array (first row) and the number of rows

void dostuff(double a[][3], int rows)
{
// access them as a[0][0] .. a[2][2]
}
int main()
{
    double stuff[3][3];
    double (*p_stuff)[3] = stuff;
    dostuff(p_stuff, 3);
}

3) pass a pointer to the first value in the first row and the number of both columns and rows

void dostuff(double a[], int rows, int cols)
{
// access them as a[0] .. a[8];
}
int main()
{
    double stuff[3][3];
    double *p_stuff = stuff[0];
    dostuff(p_stuff, 3, 3);
}

(that this last option is not strictly standards-compliant since it advances a pointer to an element of a 1D array (the first row) past the end of that array)

If that wasn't a C function, there'd be a few more options!

1
  • 1
    +1, As a slight extension to the last line, in C++ you can also pass the array by reference, and you can use templates to make the code independent of the array sizes: template <std::size_t N, std::size_t M> void process( double (&a)[N][M] ); if you want it to be able to work with different sizes. Mix and match as you wish: if you only want to allow squared arrays use a single template argument. Mar 16, 2011 at 18:12
3

Your assigned is flawed. p_stuff; is pointer to pointer to double whereas stuff is two dimensional array( array of arrays)

A single dimension array decays to the pointer to its first element. A 2 dimensional array decays to pointer to a single dimension array.

Try this

double stuff[3][3];
double (*p_stuff)[3]; // pointer to array of 3 int
p_stuff = stuff;
0
1

double ** p_stuff; corresponds to an array of pointer to double. double stuff[3][3] doesn't have any pointers - it's a 2D array of double.

1
  • Edited question to reflect my intent..getting this double stuff[3][3] to a C function.
    – Derek
    Mar 16, 2011 at 17:25
0

Below is maybe your answer:

#include <stdio.h>
#include <stdlib.h>

typedef unsigned int uint;
uint m[10][20];
uint **ppm;

int main() {
    int i;

    ppm = (uint **)m;
    for (i =0; i<10; ++i)ppm[i] = (uint *)&m[i];

    m[1][1] = 10;

    printf("0x%x vs 0x%x: %d vs %d\n", ppm,m, m[1][1], *(*(ppm+1)+1));

    return 0;
}

The result's on console screen:

0x6010a0 vs 0x6010a0: 10 vs 10
0

In addition to Cubbi's detailed answer, the following way is just natural to pass a 2-dim array to a function:

void dostuff(void *a1, int rows, int cols)
{
    double (*a)[cols] = (double (*)[cols]) a1;
    // access them as a[0][0] .. a[rows-1][cols-1];
}
int main()
{
    double stuff[3][3];
    dostuff(stuff, 3, 3);
}

You don't have to cast them in advance since the newest C++ (C++14) supports run-time sized arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.