# [C/C++] Using a double pointer to create a multidimensional array

1. May 31, 2013

### Jamin2112

Tomorrow I'm taking a programing assessment for a potential job. I was wondering whether, if I ever have to create a 2-D array for one of the challenge problems, I should use the method here: http://stackoverflow.com/questions/...nal-array-implementation-using-double-pointer.

Also, any general programming advice welcome as this'll be my first on-the-stop programming challenge. Not sure what types of things they'll have me do.

2. May 31, 2013

### Staff: Mentor

There are a couple of ways to create a 2-D array - using a pointer to memory allocated on the heap (using malloc or other memory allocation library functions), or declaring a 2-D array on the stack, as below.
Code (Text):
int array[2][3] = {{1, 2, 3}, {4, 5, 6}};
It's hard to predict what they might ask you to do in the interview, so I won't try to guess.

3. May 31, 2013

### rcgldr

The example double pointer program can be modified to perform a single allocation of memory, using an offset from the base pointer to get the offset to the first element of data, and that indexing can be used with a double pointer. The indexing can be used even if the memory is separately allocated as done in that example double pointer program.

Code (Text):

#include <malloc.h>

#define NROW 4
#define NCOL 8

int main()
{
int i, j;
int *pdata;
int **ptr;

/* (NROW * sizeof(int *))      == size of the array of pointers to rows */
/* (NCOL * sizeof(int))        == size of a row */
/* (NROW * NCOL * sizeof(int)) == size of the data */

ptr = (int **)malloc((NROW * sizeof(int *)) + (NROW * NCOL * sizeof(int)));
pdata = (int *)&ptr[NROW]; /* data starts after the last pointer to row */

for(j = 0; j < NROW; j++)   /* initialize the pointers to the rows */
ptr[j] = &pdata[NCOL*j];

for(j = 0; j < NROW; j++)   /* initialize the data = row * 0x10 + column */
for(i = 0; i < NCOL; i++)
ptr[j][i] = j * 0x10 + i;

free(ptr);

return 0;
}

A slight variation of this uses a pointer to an 2d array of integers. The memory layout is the same as the above example:

Code (Text):

#include <malloc.h>

#define NROW 4
#define NCOL 8

int main()
{
int i, j;
int (*parray)[NROW][NCOL];      /* ptr to 2d array */
int **ptr;

/* (NROW * sizeof(int *))      == size of the array of pointers to rows */
/* (NCOL * sizeof(int))        == size of a row */
/* (NROW * NCOL * sizeof(int)) == size of the data */

ptr = (int **)malloc((NROW * sizeof(int *)) + (NROW * NCOL * sizeof(int)));
parray = (int (*)[NROW][NCOL])&ptr[NROW]; /* data starts after the last pointer to row */

for(j = 0; j < NROW; j++)   /* initialize the pointers to the rows */
ptr[j] = (*parray)[j];

for(j = 0; j < NROW; j++)   /* initialize the data = row * 0x10 + column */
for(i = 0; i < NCOL; i++)
ptr[j][i] = j * 0x10 + i;

free(ptr);

return 0;
}

Mark44 posted a much simpler example of an actual 2d array. Note that pointer syntax can be used with an array, although I'm not sure why you would want do. Using Mark44's example program, *(array+1) is the same as array[1], and *(*(array+1) + 2) is the same as array[1][2].

Last edited: May 31, 2013
4. May 31, 2013

### randombill

Here you go! this program creates a 2 dimensional array using an std::vector which is then filled with the array indices and printed. Finally its deleted on program exit.

Code (Text):
#include <vector>
#include <iostream>

class A
{
private:

int i;
int j;

public:

A(){}
~A(){}
A(int i, int j)
{
// the "this" operator is a pointer to class A. Its essentially the address of the object A itself.
this->i = i;
this->j = j;
}
void print()
{
std::cout<<i<<","<<j<<std::endl;
}
};
int main()
{
//initialize vector
std::vector< std::vector <A*> > two_D;

//fill vector with elements using new
for(int i = 0; i < 3; i++)
{
two_D.push_back(std::vector <A*> ());

for(int j = 0; j < 3; j++)
{
two_D[i].push_back(new A(i,j));
}
}

//output all the data stored in each element
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
two_D[i][j]->print();
}
}

//clean up the ponters with delete so you have no memory leaks
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
delete two_D[i][j];
}
}

return 0;

}

One word of warning with C++. Phone interviews are hard to BS and knowing how to code takes years of experience I can only give you this help but there are many directions this can go.

5. May 31, 2013

### Jamin2112

It's not a phone interview. It's a timed online assessment. (Obviously it would be possible for me to consult Stack Overflow or Google things I don't remember, so I'm guessing the problems are difficult enough that I wouldn't have time to do that.)

6. May 31, 2013

### rcgldr

Good luck with the assessment. Why did you think that a 2d array might be part of the assessment? As you can see, there are various approaches, but if it's just a 2d array, then Mark44's example is the simplest since it doesn't involved pointers. If you think the assessment will involve questions about double pointers, then the other examples should help.

A more common example of a double pointer (with variable length rows which are really parameter strings), one which you've already probably used:

int main(int argc, char **argv);

argv can also be declared as an array of pointers (older C textbooks define it this way):

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

7. May 31, 2013

### Jamin2112

I have no idea what's a part of the assessment, but if I have to make a 2-d array then I was just making sure I do it the way they like in the programming world, because I know my buddy who works a job programming microprocessors uses a certain convention.

I think that's old school .... http://stackoverflow.com/questions/3898021/mainint-argc-char-argv

8. May 31, 2013

### rcgldr

It's still used for console programs in dos, linux, posix, unix, windows, and for windows apps if you add parameters to the command lines used to launch a windows program from an icon or shortcut. The point was that argv is the most common example of a double pointer or array of pointers (two ways of declaring the same thing).

In the "programming world", 2-d arrays are implemented in various way, as shown above. You can just delcare a 2-d array as a global or local variable as shown in Mark44's post. You can declare a pointer to a 2-d array and allocate memory for it (which is what parray is in my previous post). You can delcare a pointer to pointer (double pointer), allocate memory for it, and use it as an array of pointers (which is how argv from main() is used). Which way is "best" depends on the circumstances.