I have to return the array b to the main program as char **. How do I do it? If I give
return b;
it gives me an error.
In main() if I give
char **c;
c=GetRandomStrings(2,10,10);
I will get the first string. How to obtain the remaining strings?
char b[10][20];
char** GetRandomStrings(int minimumLengthOfString, int maximumLengthOfString, int numOfStrings)
{
static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char s[maximumLengthOfString];
int num =0,length;
srand((unsigned int)time(NULL));
for (int j=0;j<numOfStrings;j++)
{
length = randomLength (minimumLengthOfString,maximumLengthOfString);
for (int i = 0; i < length; ++i)
{
num = rand() % (sizeof(alphanum) - 1);
s[i] = alphanum[num];
}
s[length] = '\0';
strcpy(b[j],s);
}
for (int j=0;j<numOfStrings;j++)
{
printf("\n\n%s",b[j]);
}
return b;
}
int randomLength(int minimumLength, int maximumLength)
{
return (rand()%(maximumLength - minimumLength) + minimumLength);
}
b, then consider that you will return a pointer to a local variable, which leads to undefined behavior, and also thatbis an array of arrays of characters, which is not the same as a pointer to a pointer to character.bshould not be global, you shouldmallocit withinGetRandomStrings()bischar (*)[20], notchar**. but There is no need to return a global variable.