0

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.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by flaviusilaghi
0

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.

0

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 by flaviusilaghi: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.