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.

Recommended Answers

All 2 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.