1

文字の配列または文字列を渡す関数があります。この配列をデータ配列に使用しているため、NULL 文字を含む多くのランダムな文字が含まれています。私の問題は、コンパイラが見ているこのデータと Null char を取得しようとしていて、文字列が終了していると考えているときに発生します。その後、すべてのデータを効果的に破棄します。Null char で終了しない配列を何らかの方法で作成できるオプションはありますか?

4

3 に答える 3

7

AC 文字列との配列はchar同じものではありません。最初のものは、配列が最初の0要素を持つ場所で文字列が終了するという追加の規則を使用して、2 番目のものによって実装されます。

したがって、必要なのは だけで、必要なunsigned char[something]長さを個別に追跡する必要があります。strcpyまた、または同様の機能を使用しないでくださいmemcpy

于 2013-06-06T19:47:03.057 に答える
3

null ( '\0') ターミネータは、C では文字列ターミネータとして扱われます。そのため、コンパイラに読み取るデータの量を正確に伝える必要があります。データのサイズについて別のカウントを維持してから、それを使用する関数を使用しないでください。データを操作するサイズ?

于 2013-06-06T19:45:13.010 に答える
2

Firstly, a string in C language is not some sort of "black box" object that can somehow choose to ignore or not ignore something out of its own will. It is based on a mere raw array of chars, of which you have full unrestricted control. This means that it is really you who chooses how to process the data stored in that array of chars. Not the compiler, not the string itself, but you and only you.

Secondly, a string in C language is defined as a sequence of characters ending with zero character. This immediately means that if you attempt using string-specific functions with your array, they will always stop at zeros. If you want your data to contain embedded zeros, then you should not call it "strings" and you should not use any string-specific functions with it. So, forget about strcmp, strcpy and such. Again, it is something you are responsible for, not the compiler.

Thirdly, the functions you would use with such data would typically be functions like memcpy for copying, memcmp for comparison and so on. Anything that's missing you'll have to implement yourself. And since you no longer have any terminating characters in your data, it is your responsibility to know where the data begins and where it ends.

于 2013-06-06T20:21:55.827 に答える