1. Dynamic 2d array

Ok, I've already searched and there are a bazillion threads for this but I couldn't seem to get it working (to tired prolly is hangover) ....

Anyways I'm trying to create a dynamic 2d array of a struct.

Code:
```typedef struct{
int id;          // Cell id
HWND child;      // Cell child (either browse,edit,static or combo)
LPSTR data;      // Text or num if static
}MITHTABCELLSTRUCT, *LPMITHTABCELLSTRUCT;

typedef struct{
int cellHeight;  // The height of the cells
int cellWidth;   // The width of the cells
int xOffset;     // x offset where cells begin in parent
int yOffset;     // y offset Where cells begin in parent
int padding;     // Padding in cells (higher = smaller text, lower = larger text)
int spacing;     // Spacing between cells
int rows;        // Number of cell rows
int cols;        // Number of cell columns
MITHTABCELLSTRUCT **cells;    //The cells (2d array defined by int rows,cols;)
}MITHDATATABSTRUCT, *LPMITHDATATABSTRUCT;```
That's the structs, I'm trying to make MITHTABCELLSTRUCT **cells a 2d array. I gather ** was right because I saw it in all the examples.

It is supposed to be the size of [rows][cols] as it's going to be used to create a table. I just need to write a function to initialize the array.

If you could explain to me what I need to do rather than showing me the code because I really need to understand what I'm doing (you don't hear that often do you? )..

Cheers.

PS: Sorry for spelling/grammar mistakes, I whent to a 21st yesterday and still drunk.

[edit:]Highlighted the variable

2. Maybe there's something in the STD. But I'll admit, dynamic 2-d's a \$\$\$\$\$. Could you do a vector of vectors, maybe?

3. A 2d array is the best way for me I think, I could copy/paste code but I don't like using code if I don't know how it works .

4. MITHTABCELLSTRUCT **cells

1. Allocated memory for the rows
2. For each Row allocate memory for the column

Code:
```void InitStruct(MITHDATATABSTRUCT* s)
{
int y;

s->cells=(MITHTABCELLSTRUCT**)malloc(sizeof(MITHTABCELLSTRUCT*)*ROWS);

for(y=0;y<ROWS;y++)
s->cells[y]=(MITHTABCELLSTRUCT*)malloc(sizeof(MITHTABCELLSTRUCT)*COLS);
}```

5. Thanks.

6. Hrmm... Doesn't seem to work. Using debugger I watched the struct and it got every variable set but the cells[][] variable which remained 0x0.

7. works 4 me. Try this example

Code:
```#include <malloc.h>
#include <iostream>
#include <windows.h>
using namespace std;

#define ROWS	5
#define COLS	5

typedef struct{
int id;          // Cell id
HWND child;      // Cell child (either browse,edit,static or combo)
LPSTR data;      // Text or num if static
}MITHTABCELLSTRUCT, *LPMITHTABCELLSTRUCT;

typedef struct{
int cellHeight;  // The height of the cells
int cellWidth;   // The width of the cells
int xOffset;     // x offset where cells begin in parent
int yOffset;     // y offset Where cells begin in parent
int padding;     // Padding in cells (higher = smaller text, lower = larger text)
int spacing;     // Spacing between cells
int rows;        // Number of cell rows
int cols;        // Number of cell columns
MITHTABCELLSTRUCT **cells;    //The cells (2d array defined by int rows,cols
}MITHDATATABSTRUCT, *LPMITHDATATABSTRUCT;

void InitStruct(MITHDATATABSTRUCT* s)
{
int y;
int x;
s-> cells=(MITHTABCELLSTRUCT**)malloc(sizeof(MITHTABCELLSTRUCT*)*ROWS);

for(y=0;y<ROWS;y++)
{
s-> cells[y]=(MITHTABCELLSTRUCT*)malloc(sizeof(MITHTABCELLSTRUCT)*COLS);

for(x=0;x<COLS;x++)
s->cells[y][x].id=y*COLS+x;
}
}

int main(int argc, char* argv[])
{
MITHDATATABSTRUCT test;

InitStruct(&test);

int y,x;

for(y=0;y<ROWS;y++)
for(x=0;x<COLS;x++)
cout<<test.cells[y][x].id<<endl;

return 0;
}```

8. My mistake, it is working ... it's actually a windows programming error which is causing the out of array bounds crashing.

I think it's off to the windows programming forum now.

Thanks a bunch!

[edit:] W00t, fixed it in a stroke of genius. So no Windows forum post for me Thanks again!

9. two dimensional dynamic array?

There's some stuff I'd do differently from that post, as that's probably a year old now (IE use std::size_t instead of unsigned int and having some container typedefs and iterators, etc.). Also, a couple of posts in that thread were deleted so you might miss out on a bit of the discussion. You want to read the next to last post in that thread.

You can also use boost's multi_array

Edit: It looks like one of Salem's posts between the last 2 replies is missing. If I remember correctly, he suggested another solution which allocates one contiguous block of memory by allocating more memory than youneed for all of the array data and reinterpretting the first few elements as pointers which refered to the different rows in the array. It's another solution which I'd recommend over your current approach, but the one I mention in that thread I'd recommend over both.