Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
関数に渡された文字列定数から個々の文字を読み取り、それらを配列に入れようとしています。私が望む最終結果は、文字列定数と同じ文字列である文字列配列です。これは私のコードです:
for(int i = 0; i < string_length; i++) { sscanf(string, "%c", &array[i]); }
stringが"string"の場合、取得するのはstring_lengthサイズの配列であり、すべての値がsです。何か案は?
sscanfはインデックスの位置を記憶できず、ポインタを自動的にインクリメントします。したがって、事実上、ループ内で呼び出すたびに最初の文字を読み取ることになります。
以下のようにしてみてください
sscanf(string+i, "%c", &array[i]);
追記: strcpyまたはmemcpyを使用して、文字列を文字配列にコピーすることもできます。これにsscanfを使用する十分な理由があることを願っています。