I came across an issue when trying to save a random fruit from the array into an array of chars.
The error message reads: error: assignment to expression with array type fruit = fruits[rand() % 20];
Specifically it is these two lines that seem to be the issue:
char fruit[20];
fruit = fruits[rand() % 20];
I have tried combining it into one line such as:
char fruit[] = fruits[rand() % 20];
But this also does not work. I have tried to figure this out from others posts but I can't seem to figure out the cause. If anyone has a fix or a correct method of doing this I would greatly appreciate it. Thanks.
Full code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main () {
time_t t;
const char *fruits[20] = {"apple", "pear", "orange", "banana", "watermelon", "cantaloupe", "grape", "kiwi", "blackberry", "blueberry", "raspberry", "cherry", "strawberry", "lemon", "lime", "plum", "pineapple", "peach", "mango", "olive"};
srand((unsigned) time(&t));
char fruit[20];
fruit = fruits[rand() % 20];
printf("\nrandom fruit is %s\n", fruit);
return 1;
}