I am having a hard time putting a string into an array.
The purpose of my program is to generate random strings. And I want those random strings generated to be placed on an array so I can access those strings again later.
char main()
{
srand(time(NULL));
char* rStr[9] = {0};
int i;
for (i = 0; i < 7; i++)
{
randomString(rStr, 8); //This is the function that generates a
//random string with 8 characters
printf("%d---%s\n", i, rStr);
}
}
The above code will produce 7 randomly-generated strings with 8 characters. But my problem is I want every strings generated to be placed on an array so I can call them or display them again.
I have tried this below:
char main()
{
srand(time(NULL));
char* rStr[9] = {0};
int i;
for (i = 0; i < 7; i++)
{
rStr[i] = randomString(rStr, 8);
printf("%d---%s\n", i, rStr[i]);
}
}
but my program would just crash.
EDIT:
int randomNumber(int min, int max) //This function is responsible for
//the randomness of the string
{
max -= min;
return (rand() % max) +min;
}
char randomString(char *str, int randomCharCount)
{
const char *charSet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
for (i = 0; i < randomCharCount; i++)
{
str[i] = charSet[randomNumber(0, 35)];
}
}
Credits of the code to the tutorial right here: