我想知道在c中存储一系列句子的最佳方法是什么。在python中它很容易,但我在C中找出类似的解决方案时遇到了麻烦。
例如:
sent1="this is sentence 1"
sent2="this is sent 2"
制作清单
list = [sent1;sent2];
这个目标是打印一个随机句子
print list[random_number]
答案 0 :(得分:0)
如果句子是静态的,就像你的例子一样,你可以这样做。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t t;
const char * sentences[] = {"first sentence", "second sentence"};
srand(time(&t));
printf("%s\n", sentences[rand() % 2]);
return 0;
}
而马聪指的是K&amp; R的C编程语言:https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf