I have a 3x3 array that I'm trying to create a pointer to and I keep getting this array, what gives?
How do I have to define the pointer? I've tried every combination of [] and *.
Is it possible to do this?
int tempSec[3][3];
int* pTemp = tempSec;
I have a 3x3 array that I'm trying to create a pointer to and I keep getting this array, what gives?
How do I have to define the pointer? I've tried every combination of [] and *.
Is it possible to do this?
int tempSec[3][3];
int* pTemp = tempSec;
You can do int *pTemp = &tempSec[0][0];
If you want to treat a 3x3 array as an int*, you should probably declare it as an int[9], and use tempSec[3*x+y] instead of tempSec[x][y].
Alternatively, perhaps what you wanted was int (*pTemp)[3] = tempSec? That would then be a pointer to the first element of tempSec, that first element itself being an array.
You can in fact take a pointer to a 2D array:
int (*pTemp)[3][3] = &tempSex;
You'd then use it like this:
(*pTemp)[1][2] = 12;
That's almost certainly not what you want, but in your comment you did ask for it...
Original post follows - please disregard, it is misinformed. Leaving it for posterity's sake ;)
However, here is a link I found regarding memory allocation of 2-dimensional arrays in c++. Perhaps it may be of more value.
Not sure it's what you want, and it's been a while since I've written c++, but the reason your cast fails is because you are going from an array of arrays to a pointer of ints. If, on the other hand, you tried from array to array to a pointer of pointers, it would likely work
int tempSec[3][3]; 
int** pTemp = tempSec; 
remember, your array of arrays is really a contiguous block of memory holding pointers to other contiguous blocks of memory - which is why casting an array of arrays to an array of ints will get you an array of what looks like garbage [that garbage is really memory addresses!].
Again, depends on what you want. If you want it in pointer format, pointer of pointers is the way to go. If you want all 9 elements as one contiguous array, you will have to perform a linearization of your double array.
As Steve pointed out, the proper form is int *pTemp = &tempSec[0][0];.  int** pTemp2 = tempSec; does not work.  The error given is:
cannot convert 'int (*)[3]' to 'int**' in initialization
It's not stored as an array of pointers to arrays.  It's stored as one big vector, and the compiler hides the [a][b] = [a*rowLength+b] from you.
#include <iostream>
using namespace std;
int main()
{
    // Allocate on stack and initialize.
    int tempSec[3][3];
    int n = 0;
    for(int x = 0; x < 3; ++x)
        for(int y = 0; y < 3; ++y)
            tempSec[x][y] = n++;
    // Print some addresses.
    cout << "Array base: " << size_t(tempSec) << endl;
    for(int x = 0; x < 3; ++x)
        cout << "Row " << x << " base: " << size_t(tempSec[x]) << endl;
    // Print contents.
    cout << "As a 1-D vector:" << endl;
    int *pTemp = &tempSec[0][0];
    for(int k = 0; k < 9; ++k)
        cout << "pTemp[" << k << "] = " << pTemp[k] << endl;
    return 0;
}
Output:
Array base: 140734799802384
Row 0 base: 140734799802384
Row 1 base: 140734799802396
Row 2 base: 140734799802408
As a 1-D vector:
pTemp[0] = 0
pTemp[1] = 1
pTemp[2] = 2
pTemp[3] = 3
pTemp[4] = 4
pTemp[5] = 5
pTemp[6] = 6
pTemp[7] = 7
pTemp[8] = 8
Note that the Row 0 address is the same as the full array address, and consecutive rows are offset by sizeof(int) * 3 = 12.
Its easyier to use a typedef
typedef int  ThreeArray[3];
typedef int  ThreeByThree[3][3];
int main(int argc, char* argv[])
{
    int         data[3][3];
    ThreeArray* dPoint    = data;
    dPoint[0][2]          = 5;
    dPoint[2][1]          = 6;   
    // Doing it without the typedef makes the syntax very hard to read.
    //
    int(*xxPointer)[3]    = data;
    xxPointer[0][1]       = 7;
    // Building a pointer to a three by Three array directly.
    //
    ThreeByThree*  p1     = &data;
    (*p1)[1][2]           = 10;
    // Building a pointer to a three by Three array directly (without typedef)
    //
    int(*p2)[3][3]        = &data;
    (*p2)[1][2]           = 11;
    // Building a reference to a 3 by 3 array.
    //
    ThreeByThree&  ref1   = data;
    ref1[0][0]            = 8;
    // Building a reference to a 3 by 3 array (Without the typedef)
    //
    int(&ref2)[3][3]      = data;
    ref2[1][1]            = 9;
    return 0;
}
Another way to go about doing this, is to first create an array of pointers:
int* pa[3] = { temp[0], temp[1], temp[2] };
Then create a pointer pointer to point to that:
int** pp = pa;
You can then use normal array syntax on that pointer pointer to get the element you're looking for:
int x = pp[1][0]; // gets the first element of the second array
Also, if the only reason you're trying to convert it to a pointer is so you can pass it to a function, you can do this:
void f(int v[3][3]);
As long as the size of the arrays are fixed, you can pass a two-dimensional array to a function like this. It's much more specific than passing a pointer.
Oh. That's easy!
int aai[3][3];
int* pi = reinterpret_cast<int*>(aai);
You can actually use this awesome technique to cast it into other wonderful types. For example:
int aai[3][3];
int (__stdcall *pfi_lds)(long, double, char*) = reinterpret_cast<int (__stdcall *)(long, double, char*)>(aai);
Isn't that just swell? The question is whether it's meaningful.
You're asking how to lie to your compiler. So the first thing to know is: Why do you want to lie?
Let's ask cdecl.org to translate your declaration for us:
int tempSec[3][3]
returns
declare tempSec as array 3 of array 3 of int 
Ok, so how do we create a pointer to that? Let's ask cdecl again:
declare pTemp as pointer to array 3 of array 3 of int
returns
int (*pTemp)[3][3]
Since we already have the array 3 of array 3 of int, we can just do:
int (*pTemp)[3][3] = &tempSec;