I need a working code for a function that will return a random string with a random length.
What I want to do would be better described by the following code.
char *getRandomString()
{
char word[random-length];
// ...instructions that will fill word with random characters.
return word;
}
void main()
{
char *string = getRandomString();
printf("Random string is: %s\n", string);
}
For this, I am strictly forbidden to use any other include than stdio.h. Edit: This project will be adapted to be compiled for a PIC Microcontroller, hence I cannot use malloc() or such stuff. The reason why I use stdio.h here, is for me to be able to inspect the output using GCC.
Currently, this code gives this error.-
“warning: function returns address of local variable [enabled by default]”
Then, I thought this could work.-
char *getRandomString(char *string)
{
char word[random-length];
// ...instructions that will fill word with random characters.
string = word;
return string;
}
void main()
{
char *string = getRandomString(string);
printf("Random string is: %s\n", string);
}
But it only prints a bunch of nonsense characters.
If you are not allowed to use
malloc
you'll have to declare an array that can be the maximum possible size at file scope and fill it with random characters.It points to nonsense characters because you are returning local address.
char word[random-length];
is defined local tochar *getRandomString(char *string)
Dynamically allocate the string with
malloc
, populate string, and return the returned address bymalloc
. This returned address is allocated from the heap and will be allocated until you do not manually free it (or the program does not terminate).After you have done with the allocated string, remember to free the string.
Or another thing can be done, if you cannot use
malloc
which is define the local string in thegetRandomString
asstatic
which makes the statically declared array's lifetime as long as the program runs.Or simply make the
char word[128];
global.There are three common ways to do this.
Have the caller pass in a pointer to (the first element of) an array into which the data is to be stored, along with a length parameter. If the string to be returned is bigger than the passed-in length, it's an error; you need to decide how to deal with it. (You could truncate the result, or you could return a null pointer. Either way, the caller has to be able to deal with it.)
Return a pointer to a newly allocated object, making it the caller's responsibility to call
free
when done. Probably return a null pointer ifmalloc()
fails (this is always a possibility, and you should always check for it). Sincemalloc
andfree
are declared in<stdlib.h>
this doesn't meet your (artificial) requirements.Return a pointer to (the first element of) a static array. This avoids the error of returning a pointer to a locally allocated object, but it has its own drawbacks. It means that later calls will clobber the original result, and it imposes a fixed maximum size.
None if these is an ideal solution.
As I understand, malloc is not an option.
Write a couple of functions to a) get a random integer (strings length), and b)a random char.
Then use those to build your random string.
For example:
Both of your examples are returning pointers to local variables - that's generally a no-no. You won't be able to create memory for your caller to use without
malloc()
, which isn't defined instdio.h
, so I guess your only option is to makeword
static or global, unless you can declare it inmain()
and pass the pointer to your random string function to be filled in. How are you generating random numbers with only the functions instdio.h
?