Can I create variables during compiling time in in c++

Feb 28, 2018 at 4:46am
I appreciate it if someone has the answer.

Can I create variables during compiling time in in c++. I have an array with 729 elements. I want to divide it into 81 arrays each consists of 9 elements. I do not want to declare 81 arrays. I am thinking maybe there is a way I can use a "for" loop to do it for me.
Feb 28, 2018 at 5:19am
The title of your post poses an XY problem (see: http://xyproblem.info/ ). Fortunately you have provided a little information about your actual problem.

Consider declaring an array of arrays. int x[81][9]; // 81 arrays of 9 int;
Or using pointer arithmetic.
1
2
int* block_n(int* x, int n) 
{ return x + (n * block_size) }
More sophisticated solutions are available if required.
Last edited on Feb 28, 2018 at 5:21am
Feb 28, 2018 at 9:32am
If you already have defined and initialized the array of 729 elements ...

 
int x[729] = { 1, 2, 3, ..., 728, 729 };

... then you can easily split it up into an array of arrays without having to touch the sequence of values.

 
int x[81][9] = { 1, 2, 3, ..., 728, 729 };
Last edited on Feb 28, 2018 at 9:32am
Topic archived. No new replies allowed.