CHAR_INFO vector

I am taking a 2d charinfo vector and trying to convert it to a dynamic 2d CHAR_INFO array to pass to a function. I am not sure how i would copy this, and the other problem, is i have this code for my copy. The code may be for windows, but the concept is standard.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <windows.h>
int main()
{ //Imagine you had stuff in mainvector
int width=10;
int height=10;
std::vector <std::vector < CHAR_INFO > > mainvector;
//Resize loop
mainvector.resize(height); //Yes, this is correct, in char_info arrays
for(int i; i<width; i++)        //It needs to be height, width, to the function
mainvector[i].resize(width);
CHAR_INFO * array = new CHAR_INFO[height][width];
for(int countery; countery<height; countery++)
for(int counterx; counterx<width; counterx++)
array[countery][counterx]=mainvector[countery][counterx];
    COORD dwBufferSize = { width, height }; //This doesn't quite matter
    COORD dwBufferCoord = { 0, 0 };
    SMALL_RECT rcRegion = { 0, 0, width, height };
    HANDLE hOutput = (HANDLE)GetStdHandle( STD_OUTPUT_HANDLE );
    WriteConsoleOutput( hOutput, (CHAR_INFO *)array, dwBufferSize,
    dwBufferCoord, &rcRegion );
}


However, my MinGW Codeblocks setup is saying that width and height need to be constants.

error: array size in operator new must be constant

Please Help
Last edited on
std::vector <std::vector < CHAR_INFO > > mainvector;
ยด
How can you create a vector of CHAR_INFO before telling the program what CHAR_INFO is, or am I missunderstanding something?
Char info is a struct defined by windows.
&TarikNeaj CHAR_INFO is part of the windows.h header file. However, the op does need to include vector.
Last edited on
new CHAR_INFO[height][width]; in this case width should be a compile time constant.

You can do nested allocation to circumvent it, but this would not be true 2D array expected by WriteConsoleOutput.

So only way is to allocate array of size width*height
Read more there (Killing MD: 1D Mimics): http://www.cplusplus.com/forum/articles/17108/
Sorry, i hadnt posted all the code, i just wrote an example of what i was trying to do, but i am getting the error

error: array size in operator new must be constant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

using namespace std;

int main() {
	int w = 10, h = 10;
	vector<vector<char>> vvc(w);
	for(auto& a : vvc)
		a.resize(h, 'x');
		
	char** ppc = new char*[h];
	
	for(int i = 0; i < h; ++i)
	{
		ppc[i] = new char[w];
		for(int j = 0; j < w; ++j)
		{
			ppc[i][j] = vvc[i][j];
		}
	}
	
	for(int i = 0; i < h; ++i)
	{
		for(int j = 0; j < w; ++j)
		{
			cout << ppc[i][j];
		}
		cout << endl;
	}	
}
http://ideone.com/XoPmnC
Thank you, that worked perfectly
Just to warn that naraku9333 example should not be used with WriteConsoleOutput and similar functions.
OK then, what way could I make it to pass to a function like that?
Probably something like CHAR_INFO* ci = new CHAR_INFO[w * h]; See article MiiNiPaa linked to.
Topic archived. No new replies allowed.