如何在c中存储字符串数组中的随机字符串?

如何在c中存储字符串数组中的随机字符串?,c,arrays,string,char,C,Arrays,String,Char,我在尝试将数组中的随机水果保存到字符数组中时遇到了一个问题 错误消息如下:error:assignment to expression with array type 水果=水果[rand()%20] 具体而言,这两条线似乎是问题所在: char fruit[20]; fruit = fruits[rand() % 20]; 我尝试将其合并为一行,例如: char fruit[] = fruits[rand() % 20]; 但这也不起作用。 我曾试图从其他帖子中找出这一点,但我似乎无法找出

我在尝试将数组中的随机水果保存到字符数组中时遇到了一个问题

错误消息如下:error:assignment to expression with array type 水果=水果[rand()%20]

具体而言,这两条线似乎是问题所在:

char fruit[20];
fruit = fruits[rand() % 20];
我尝试将其合并为一行,例如:

char fruit[] = fruits[rand() % 20];
但这也不起作用。 我曾试图从其他帖子中找出这一点,但我似乎无法找出原因。如果有人有一个修复或正确的方法来做这件事,我将非常感谢。谢谢

完整代码:

#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;
}
#包括
#包括
#包括
int main(){
时间;
const char*果品[20]={“苹果”、“梨”、“橙子”、“香蕉”、“西瓜”、“哈密瓜”、“葡萄”、“猕猴桃”、“黑莓”、“蓝莓”、“覆盆子”、“樱桃”、“草莓”、“柠檬”、“酸橙”、“李子”、“菠萝”、“桃”、“芒果”、“橄榄”};
srand((未签名)时间(&t));
炭果[20];
水果=水果[rand()%20];
printf(“\n随机水果是%s\n”,水果);
返回1;
}
复制C字符串时,不使用赋值,因为这将尝试复制指向字符串的指针。试图将其复制到数组变量是导致问题的原因

相反,请使用
string.h
工具:

strcpy (fruit, fruits[someRandomIndex]);
并且确保你永远不要在水果列表中添加
golden delicious apple
,因为它很容易覆盖19个字符的限制:-)

但是,事实上,您实际上不需要复制字符串,因为您可以将原始指针传递到
printf
。您还可以清理代码,以便于维护:

#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;
}
#包括
#包括
#包括
内部主(空){
//指向常量数据的常量指针通常允许更多
//优化范围。也可自动调整阵列大小。
静态常量char const*fruits[]={
“苹果”、“梨”、“橘子”、“香蕉”、“西瓜”,
“哈密瓜”、“葡萄”、“猕猴桃”、“黑莓”,
“蓝莓”、“覆盆子”、“樱桃”、“草莓”,
“柠檬”、“酸橙”、“李子”、“菠萝”、“桃子”,
“芒果”、“橄榄”};
静态常数int FROUTCOUNT=sizeof(水果)/sizeof(*水果);
//种子发生器,无需储存,只需使用即可。
srand((未签名)时间(0));
//根据大小获取随机指针,并打印它。
printf(“随机水果是%s\n”,水果[rand()%fruitCount]);
//返回通常的成功代码。
返回0;
}
复制C字符串时,不使用赋值,因为这将尝试复制指向字符串的指针。试图将其复制到数组变量是导致问题的原因

相反,请使用
string.h
工具:

strcpy (fruit, fruits[someRandomIndex]);
并且确保你永远不要在水果列表中添加
golden delicious apple
,因为它很容易覆盖19个字符的限制:-)

但是,事实上,您实际上不需要复制字符串,因为您可以将原始指针传递到
printf
。您还可以清理代码,以便于维护:

#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;
}
#包括
#包括
#包括
内部主(空){
//指向常量数据的常量指针通常允许更多
//优化范围。也可自动调整阵列大小。
静态常量char const*fruits[]={
“苹果”、“梨”、“橘子”、“香蕉”、“西瓜”,
“哈密瓜”、“葡萄”、“猕猴桃”、“黑莓”,
“蓝莓”、“覆盆子”、“樱桃”、“草莓”,
“柠檬”、“酸橙”、“李子”、“菠萝”、“桃子”,
“芒果”、“橄榄”};
静态常数int FROUTCOUNT=sizeof(水果)/sizeof(*水果);
//种子发生器,无需储存,只需使用即可。
srand((未签名)时间(0));
//根据大小获取随机指针,并打印它。
printf(“随机水果是%s\n”,水果[rand()%fruitCount]);
//返回通常的成功代码。
返回0;
}
使用strcpy(来自“string.h”):

但是,如果您的结果只需要是一个常量字符串,并且您不会更改它,那么只需将其声明为指针
const char*fruit
并且您可以像以前一样完成作业。

使用strcpy(来自“string.h”):

但是,如果您的结果只需要是一个常量字符串,并且您不会更改它,那么只需将其声明为指针
const char*fruit并且您可以像以前一样完成作业