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

In summary, there are multiple ways to create a 2-D array, including using a pointer to memory allocated on the heap or declaring an array on the stack. It is difficult to predict what may be asked in a programming challenge, so it is best to be familiar with different methods. The provided conversation also includes examples of creating a 2-D array using a single allocation of memory and using a pointer to a 2-D array of integers. Additionally, a simpler example using an std::vector is shown.
  • #1
Jamin2112
986
12
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. :smile:
 
Technology news on Phys.org
  • #2
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:
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
Jamin2112 said:
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:
#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:
#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:
  • #4
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:
#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
randombill said:
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.


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
Jamin2112 said:
It's a timed online assessment.
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
rcgldr said:
Good luck with the assessment. Why did you think that a 2d array might be part of the assessment?

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.

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[]);

I think that's old school ... http://stackoverflow.com/questions/3898021/mainint-argc-char-argv
 
  • #8
rcgldr said:
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[]);

Jamin2112 said:
I think that's old school.
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).

Jamin2112 said:
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
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.
 

Question 1: What is a double pointer in C/C++?

A double pointer is a pointer that points to another pointer. It is used to store the memory address of a pointer variable, which in turn stores the memory address of another variable.

Question 2: How do I declare a double pointer in C/C++?

To declare a double pointer in C/C++, you need to use two asterisks (**). For example, int **ptr would declare a double pointer named ptr that points to an integer.

Question 3: How do I use a double pointer to create a multidimensional array?

To create a multidimensional array using a double pointer, you need to first create an array of pointers, each pointing to another array. Then, you can use nested loops to access and manipulate the elements of the multidimensional array.

Question 4: How do I allocate memory for a multidimensional array using a double pointer?

To allocate memory for a multidimensional array using a double pointer, you need to use the malloc() or calloc() function. You will need to specify the total size of the array, taking into account the size of each element and the number of dimensions.

Question 5: How do I free the memory allocated for a multidimensional array created using a double pointer?

To free the memory allocated for a multidimensional array created using a double pointer, you need to use the free() function. You will need to pass in the address of the first element of the array, which is the same as the address stored in the double pointer variable.

Similar threads

  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
882
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top