0

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;
}
0

2 Answers 2

1

When you're copying C strings, you don't use assignment, since that will attempt to copy the pointer to the string. Trying to copy that to an array variable is what's causing your issue.

Instead, use the string.h facilities:

strcpy (fruit, fruits[someRandomIndex]);

And make sure you never add golden delicious apple to your list of fruits, since it will easily overwrite the 19-character limit :-)

But, in fact, you don't actually need to copy the string since you're quite able to pass the original pointer to printf. You can also clean up your code so as to make maintenance easier:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {
    // Const pointer to const data generally allows more
    // scope for optimisation. Also auto size array.

    static const char const *fruits[] = {
        "apple", "pear", "orange", "banana", "watermelon",
        "cantaloupe", "grape", "kiwi", "blackberry",
        "blueberry", "raspberry", "cherry", "strawberry",
        "lemon", "lime", "plum", "pineapple", "peach",
        "mango", "olive"};   
    static const int fruitCount = sizeof(fruits) / sizeof(*fruits);

    // Seed generator, no need to store, just use it.

    srand ((unsigned) time(0));

    // Get random pointer based on size, and print it.

    printf ("Random fruit is %s\n", fruits[rand() % fruitCount]);

    // Return usual success code.

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use strcpy (from "string.h"):

strcpy(fruit, fruits[rand() % 20]);

However, if your result needs only to be a constant string and you wont change it, declare it as a pointer simply const char* fruit; and you can do the assignment as you did.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.