English 中文(简体)
C区已终止的地狱
原标题:Null terminated string in C
  • 时间:2011-11-20 16:25:57
  •  标签:
  • c
  • string
  • null

我非常关心C的扼杀。 我是否需要确定最后的char,还是由它自己决定? 如果我不以人工的方式行事,那么当我试图推翻法典时,当我看看着1[257]号指示时,这并非无效。 我在解开一系列的雕像时有问题,因此我认为这是一个原因。

char string1[257], string2[257];
scanf("%s", &string2);
string1[257] =   ;
strncpy(string1, string2, 257);
string1[257] =   ; /* do I need to do that? */
最佳回答

是否绝对必要? <>No,因为在您打电话scanf时,strcpy(strncpy除外。 如果你在体力上填满了零件,就把零件复印给你。 这样做是否好? <>Nostrong>No really, 它确实无助于缓冲外流问题,因为这些功能将超越缓冲地带的规模。 那么,什么是最佳方法? 使用<代码>的++:载。

顺便提一句,如果您能够查阅<>条码>,则该编码将不受约束,因为您可重新查阅/撰写第258条<>第号<>在一系列大小257中的内容。 (零基指数)

问题回答

封面字像>“Hello World >/code> 无效,但char 阵列并不自动终止。

始终采取的一般原则一是谨慎行事,并将<条码>>>>转让至扼杀结束,除非造成履约问题。 在这些情况下,我对图书馆的职能是我使用的额外谨慎。

A literal string like "foo bar" is always translated to a const char literal[] with an additional zero byte at the end. (So the constant would have 8 bytes, the first being f and the last being zero).

但是,在<代码>strncpy后,你有权明确将最后的 by升至0。

正如Aurelio De Rosa所说,最后一个正确的指数是256个阵列<代码>[257]。

是的,你需要这样做。 并非所有职能都给你留下了独一无二的char子,而且正如我可以在一页读的那样,<条码>斯特凡·要求rc首的独一无二。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签