gardian06
gardian06

Reputation: 1546

initializing a multi dimensional array c++

I am working in VC++ 2008, and am trying to allocate a multi-dimensional array of chars to do some file work. I know that whenever an array is allocated all the members of the array should be initialized usually in a successive order. what I have currently is this.

char ** thing = new char *[lineY];
for (int ii = 0; ii < lineY; ii++){
    thing[ii] = new char[lineX];
}
... // working with array
// deleting each part of the array.
for (int ii = 0; ii < lineY; ii++){
    delete [] thing[ii];
}
delete [] thing;

the problem that I am running into is that if I add the array to the watch list, or put a break right after its been allocated the debugger states that the array is equal to a number like 51, or 32, and not a block of space with indexes, and values, but when I try to initialize the values of each index by making my allocation this:

char ** thing = new char *[lineY];
for (int ii = 0; ii < lineY; ii++){
    thing[ii] = new char[lineX];
        for (int jj = 0; jj < lineX; jj++){
        thing[ii][jj] = '';
    }
}

edit: the compiler throws "C2137 empty character constant" am I doing something wrong? edit: read msdn on the error number, and found answer

Upvotes: 0

Views: 296

Answers (2)

fredoverflow
fredoverflow

Reputation: 263118

Are lineX and lineY compile-time constants? In that case:

std::array<std::array<char, lineX>, lineY> thing;

Otherwise:

std::vector<std::vector<char> > thing(lineY, std::vector<char>(lineX));

Upvotes: 0

Pulkit Goyal
Pulkit Goyal

Reputation: 5654

You cannot write thing[ii][jj] = '' because '' is an empty character constant which isn't allowed. Try replacing '' with something like ' ' (with the space between 's)

Upvotes: 1

Related Questions