954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

multidimensional array

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.

flaviusilaghi
Newbie Poster
10 posts since Jun 2010
Reputation Points: 10
Solved Threads: 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.

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

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

flaviusilaghi
Newbie Poster
10 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: