Reputation: 1113
I am currently trying to use a random string generator to produce a string. It looks like this:
char *randstring(int length) {
static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
char *randomString = NULL;
int n = 0;
if (length) {
randomString = malloc(sizeof(char) * (length +1));
if (randomString) {
for (n = 0;n < length;n++) {
int key = rand() % (int)(sizeof(charset) -1);
randomString[n] = charset[key];
}
randomString[length] = '\0';
}
}
return randomString;
}
I am trying to call it like this:
srand(time(NULL));
int r = rand()%1000;
char *string[1000];
&string = randomstring(r);
But when I do this, I get the following error:
error: invalid lvalue in assignment.
I have looked online but I can not figure out why this is not working. Any suggestions? It has to do with the pointer, I assume.
Upvotes: 2
Views: 8007
Reputation: 3698
&string = randomstring(r);
should be
string[0] = randomstring(r);
Upvotes: 3
Reputation: 81936
Most likely, the correct code is:
char *string = randomstring(52);
If for some reason you wanted to keep the array of character pointers, you could also do:
char *string[1000];
string[0] = randomstring(102);
Upvotes: 6