Char

if i have a buch of char variables like char p1,p2...p35 and i want to assign each one a random number how can i do that.
 
 
I cant use arrays by the way
If you can't use arrays, can you use vectors?
i doubt it
http://www.cplusplus.com/reference/cstdlib/rand/

1
2
3
4
5
6
7
char p1 = rand();
char p2 = rand();
char p3 = rand();
...
...
...
char p35 = rand();


Many problems with this. You want to assign a random NUMBER to a char? That makes no sense.

They're all called p-something. Are these meant to be pointers to char?

How did you end up with 35 char variables named p1 to p35? That's crazy.
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
1
2
    return(0);
    }


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!!!
Last edited on
Please don't do that. Clearly, if the OP isn't meant to use arrays, doing some undefined behavior hacks isn't what his instructor wants him to do.

I really doubt the instructor is telling him to define 35 char variables all named p1, p1,... p35.

@fivestar, please describe what you're actually trying to accomplish and why you need these 35 variables... please me more descriptive, because your current problem statement makes no practical sense.
Last edited on
I actually did my best to say it shouldn't be done but it could work on that specific case and on that specific machine.
But actually, aside from typing 35 lines of code with declaration and assignment, there's no way to achieve what he asks for except working with pointers.

Most likely he has just misread or misunderstood the text of his assignment. And as soon as i've read it i had a bit of nostalgia for my first year pointer arithmetics assignments, my bad xD

@fivestar: what if you send us the actual text of your assignment before i come up with some assembly suggestions?
I’m basically supposed to create a grid with 36 characters in order to play the memory game .
I see nothing in your statements which does not allow you to use arrays in the first place...
It’s just one of the directions. We can’t use arrays.
Well if it's that clear the only solution is setting them one by one :\
Or be creative and use a string, but that'd be like cheating since a string IS an array...
Last edited on
repeater, a char is just a small int. Some code uses chars as an integer type, and a random value for that makes sense. As letters/text, it does not.

it does not matter but it should be
p1 = rand()%128 or %256 if you want to make it unsigned char (mostly this removes a size mismatch warn).

I believe you could write a macro that can adjust the variable's name via the loop variable and do them all that way in a small block of code.

@fivestar
Here's a program that outputs your desired program's source code.

main.cpp:
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 <fstream>
#include <string> 

int main()
{
    std::ofstream f("out.cpp");
    f << "#include <cstdlib>\n";
    f << "#include <ctime>\n";
    f << "#include <iostream>\n";
    f << "\n";
    
    int num_char_variables = 35;
    
    std::string indent = "    ";
    f << "int main()\n{\n";
    f << indent << "srand(time(NULL));\n";
    
    for (int i = 0; i < num_char_variables; i++)
    { 
        f << indent << "char p" << i+1 << " = rand()%128;\n";
    }
    
    f << "\n";
    
    for (int i = 0; i < num_char_variables; i++)
    { 
        f << indent << "std::cout << p" << i+1 << " << std::endl;\n";
    }
    f << "}\n";
}


Compile and run it with the following commands:
(this is Windows but you can easily apply it to Linux)

g++ main.cpp -o main.exe
main
g++ out.cpp -o out.exe
out
Last edited on
Registered users can post here. Sign in or register to post.