English 中文(简体)
为什么我们只需要一个在C的扼杀装置?
原标题:Why do we need a null terminator only in strings in C?

页: 1 见第2周。 我的问题是:为什么我们需要在扼杀中独一无二的,以标志其结束,而我们不需要它在正常的果园或像一个阵列这样的非扼杀性数据类型的阵列中,就像同时印刷两个阵列(例如,废果阵阵阵列和暗阵列)那样,第二阵列的末点是什么?

I tried to demonstrate how strings are implemented for myself with some code:

this code worked printing "hi!" in the terminal

https://i.stack.imgur.com/zqher.png” rel=“nofollow noreferer”> 这还印刷了三个记分

Why in the first code did we need an additional place in the array for the null character? Couldn t we have used i < 3 instead as we did in the second code? A character array, like any other array, has a specific length, so what changed when we decided to treat string as a character array?

问题回答

Why do we need a null terminator

说明期限。


在行使职能时,string<>em>string<>>>> or 2) a array,cannot 即接受方言或阵列。 它可以在座标或阵列上点<>>。 这将是扼杀或阵列的头一种特性。

如今,该功能如何知道现在时间过长?


strings上,该功能通过检查数据,并在发现null nature时,知道时间已到底。 无需向该职能再分配任何参数。

foo_string(pointer_to_string_beginning);

arrays之后,打电话者除点人外(在规定的命令中)还必须将阵列的元件数发送给该职务。 该功能不能使用阵列的数据来了解最终结果,因为没有保留任何价值来表示“端”。

foo_array(element_count_of_the_array, pointer_to_array_beginning);

如果发送了2个参数,则使用阵列和大小。 案文使用1参数进行扼杀。

案文strings是1970年代以来采用的共同办法。


如何回馈价值,在下一个关切中显示一个阵列或阵列,但在此尚未讨论。

审议<编码>argv[][]。

当援引<条码>main(t argc, char *argv[] >时,即为阵列(指符号)所载有效要素数目。 然而,每条插图都会有你可以想象的长处。 每一座标的结尾处都标有“NUL”()。

由于Cstring的定义是,Cstring将以NUL结束,任何接受方言开始地址的职能都能够相信,它可在必要时援引<代码>strlen(str)确定其长度。

Imagine the difficulty of passing each of length for a program expecting something like:
cat file1 filenamed2 anotherfilename andEvenMore > junk where:
argv[0] -> "cat"
argv[1] -> "filenamed2"
argv[2] -> "anotherfilename"
argv[3] -> "andEvenMore"
argv[4] = NULL (this array ends with a NULL pointer)

argv[]起,与NCL点子公司终止。 替代:

for( int i = 0; i < argc; i++ )
    printf( "%s
", argv[i] );

是:

for( int i = 0; argv[i] != NULL; i++ )
    printf( "%s
", argv[i] );

点人<>(不是一系列特性)的传导也告终止。





相关问题
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 "...

热门标签