EDIT:
Do not read what follows unless it's for personal curiosity
____________________________________________________
if you can't use arrays or other structures there's a very VERY unsafe way. It will most likely work on your machine only. It's using pointers arythmetics, and it ASSUMES the allocation will be contiguous, which is NOT to be assumed in any real code.
1) do some experiments to find out if your computer allocates ascending or descending (aka if the memory address where variables are allocated is an increasing number or a decreasing number.
To do so have the adress print out:
1 2 3 4 5 6
|
int main()
{
char a;
char b;
std::cout<<"a: "<<&a<<", b: "<<&b<<std::endl;
}
|
You will have 2 numbers. These numbers will tell you if allocation is ascending or descending.
now write your program, first of all declare the variables:
1 2 3 4 5 6 7
|
int main()
{
char p1;
char p2;
...
char p34;
char p35;
|
then let's populate them with a for loop. I will now suppose they are ascending, meaning the adress of "a" in previous test was a smaller number than the adress of "b"
1 2 3 4
|
for(int i=0; i<36; i++)
{
*(&p1+(i*sizeof(char))) = //your random number
}
|
If the adresses were discending you would have done:
1 2 3 4
|
for(int i=0; i<36; i++)
{
*(&p35-(i*sizeof(char))) = //your random number
}
|
and close
Note: this is amazingly unsafe, don't use it in production code. It's single-machine-dependant. Bad thing.
Still it has some curious concepts showing off. AND i hope it works, didn't test it out and i'm not fresh with pointers tbh.
What it should do in that mysterious line, someone correct my code if i did it bad, but i'll tell you the concept.
*(&p1+(i*sizeof(char)))
&p1
let's take the adress of the first variable.
&p1+(i*sizeof(char))
then we add to that adress the size in memory of a char variable i times. For example adding it 1 time you will obtain the memory adress of the spot right after where p1 is in your memory
*(adress)
this way you go back analyzing the value contained in that adress instead of working with the adress itself like we were doing
*(adress) = something
since we're back with the value instead of the adress, something will be assigned to that value, in that adress. Hence we're assigning something to the place in memory which comes right after p1. That place in memory is the one i'm assuming to be the place where your computer will allocate the second variable, p2. This goes on in the cycle.
Enjoy pointers arithmetics!!!