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

  • Context: C/C++ 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Array Multidimensional
Click For Summary

Discussion Overview

The discussion revolves around the methods for creating a two-dimensional array in C/C++, particularly in the context of preparing for a programming assessment. Participants explore various approaches, including the use of double pointers, stack allocation, and standard vectors, while also sharing general programming advice for coding interviews.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant inquires about the best method to create a 2-D array for a programming assessment, referencing a Stack Overflow post on double pointers.
  • Another participant outlines two methods for creating a 2-D array: using heap allocation with pointers or declaring a static array on the stack.
  • A participant suggests modifying a double pointer example to perform a single memory allocation, explaining how to access elements using offsets.
  • Another example using `std::vector` is provided, demonstrating how to create a 2-D array and manage memory with dynamic allocation and deletion.
  • Some participants express uncertainty about the specific requirements of the assessment and the likelihood of encountering 2-D arrays in the questions.
  • There is mention of a simpler example of a 2-D array that does not involve pointers, which some participants argue may be preferable for certain contexts.
  • Discussion includes the common usage of double pointers in the context of command-line arguments, with references to older C conventions.

Areas of Agreement / Disagreement

Participants express various methods for creating 2-D arrays, with no clear consensus on the best approach. Some prefer simpler implementations, while others advocate for more complex methods involving pointers. The discussion remains unresolved regarding the optimal strategy for the programming assessment.

Contextual Notes

Participants highlight that the choice of method may depend on specific requirements or conventions in different programming environments, and there is uncertainty about the exact nature of the assessment questions.

Who May Find This Useful

Individuals preparing for programming assessments, particularly in C/C++, and those interested in different methods for implementing multi-dimensional arrays.

Jamin2112
Messages
973
Reaction score
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
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.
 
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:
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.
 
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.)
 
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[]);
 
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
 
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.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
11K
Replies
5
Views
2K
Replies
3
Views
4K