2016-04-07 6 views
-4

私は、cで一連の文を格納するのが最善の方法であると思っています。 Pythonで、それは非常に簡単ですが、私は悩み例えばC.Cで文のリストを作成するには

で同様のソリューションを考え出す持っています:この目標は、印刷するリスト

list = [sent1;sent2]; 

を作るために

sent1="this is sentence 1" 
sent2="this is sent 2" 

をランダムな文章

print list[random_number] 
+0

あなたの文字列リテラルであると仮定し、 'のconst char型* '値の配列を使用します。そうでなければ、はるかに複雑になります。 –

+0

K&2第2版の5.6章を参照してください。 –

+0

K&2とは何ですか? 私はconst char * list [2] = [sent1、sent2]をしますか? –

答えて

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

そしてコングMaがK & RによってCプログラミング言語を参照している:https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf

関連する問題