What is the correct way to declare a multidimensional array. I tried like this

unsigned char** intext=new unsigned char*[16];

but i get a bad_ptr.

What do you mean by "bad_ptr"? Edward doesn't have any problems with that syntax.

#include <iostream>
#include <iomanip>

int main()
    const int n = 16;
    unsigned int **intext;
    intext = new unsigned int*[n];
    for (int i = 0; i < n; i++)
        intext[i] = new unsigned int[n];

    for (int i  = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            intext[i][j] = i + j;

    for (int i  = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            std::cout << std::setw(4) << intext[i][j];
        std::cout << '\n';

    for (int i = 0; i < n; i++)
        delete[] intext[i];
    delete[] intext;

Maybe you are not allocating memory to each row.

yes ,that solved it . Thanks a lot.I did not allocate memory to each row. I didn't knew i have to do that :P

Edited 6 Years Ago by flaviusilaghi: n/a

This question has already been answered. Start a new discussion instead.