dimanche 27 juin 2021

how does new operator works in this C++ escape code expression must have a constant value

I tried to create an array like this

#include <iostream>

int main()
{
    int row, col;
    int arr[row][col]; //expression must have a constant value
}

but that didn't work so searched and then I found this code

    int row, col;
    std::cin >> row >> col;
    int** arr = new int* [row];
    for (int i = 0; i < row; i++)
        arr[i] = new int[col];

    // use the array

    //deallocate the array
    for (int i = 0; i < row; i++)
        delete[] arr[i];
    delete[] arr;

can someone explain this code to me and say how does it work?

Aucun commentaire:

Enregistrer un commentaire