Home > Net >  C Creating Dynamic 2D Array With One Statement but Without auto
C Creating Dynamic 2D Array With One Statement but Without auto

Time:07-22

I've seen that a dynamic 2D array in C can be created as follows:

auto arr{ new int[nRows][nCols] };

nRows and nCols are compile-time known and the size of the array will not change during runtime.

I've tested what is the type of arr is PAx_i (where x is nCols). But I cannot figure out what to put instead of auto (if I don't want to use it) to create a dynamic 2D array with a single statement.

So, the question: Is it possible in C to specify the type of a dynamic 2D array directly (C-style like)? If yes, how?

CodePudding user response:

C does not support dynamically-sized raw arrays (aka Variable Length Arrays, or VLAs). Whenever you come across the need for such a dynamic array (how ever many dimensions it may have), you should be immediately thinking of using the std::vector container.

Once properly created, you can use the [] operator (concatenated, for 2-D vectors) in much the same way as you would with raw arrays.

Here's a short code demo that creates a dynamic, 2-dimensional 'array' of integers, using the std::vector class, and initializes all elements with an especially significant, non-zero value:

#include <iostream>
#include <vector>

int main()
{
    size_t nCols, nRows;
    std::cout << "Enter nRows and nCols: ";
    std::cin >> nRows >> nCols;
    if (nCols < 2 || nRows < 2) {
        std::cout << "Matrix is too small!\n";
        return 1;
    }
    // The following SINGLE LINE declares and initializes the matrix...
    std::vector<std::vector<int>> arr(nRows, std::vector<int>(nCols, 42));
    std::cout << "nRows = " << arr.size() << "\n";
    std::cout << "nCols = " << arr[0].size() << "\n";
    for (auto& row : arr) {
        for (auto i : row) {
            std::cout << i << " ";
        }
        std::cout << std::endl;
    }
    // Demo for how to use the "[][]" operator ...
    arr[0][0] = arr[nRows - 1][nCols - 1] = 33; // Change 1st and last
    std::cout << "------------\n";
    for (auto& row : arr) {
        for (auto i : row) {
            std::cout << i << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}

One of the great benefits of using std::vector over new[] is that you don't have to worry about subsequently calling delete[] – the container class takes care of all memory allocation and deallocation internally.

CodePudding user response:

In C try to avoid new/delete unless you have no other choice. Next up is std::make_unique (or std::make_shared). For dynamic arrays C has 'std::vector'

Like this :

#include <vector>
#include <iostream>

int main()
{
    // 2x2 vector
    std::vector<std::vector<int>> values{ {0,1},{2,3},{4,5} };
    std::cout << values[1][1];
    return 0;
}

CodePudding user response:

Thusly, vector of vector, in this case the values are uninitialized.

std::vector<std::vector<int>> arr(nRows,std::vector<int>(nCols));

You can also do this with gcc compiler, but its not per standard, and won't work if nRows or nCols is variable in Visual Studio:

int arr[nRows][nCols];

It's better for you to get comfortable with C standard library, as it will open up new worlds for you.

Another way:

int *arr = new int[nRow*nCol];

You can then index into the flat buffer like: arr[0][1]

  • Related