[英]What is the purpose of array[index] = 0;?
char dev[20] = "dev_name";
char dest[32];
strncpy(dest,dev,sizeof(dev));
dest[sizeof(dev)-1] = 0;
What does dest[sizeof(dev)-1] = 0;
dest[sizeof(dev)-1] = 0;
means? 手段?
Does it mean all the element of that array are assigned zero?
这是否意味着该数组的所有元素都分配了零?
No it does not mean this. 不,这并不意味着这个。
Assuming you meant strncpy(dest,dev_name,sizeof(dev_name)); /* Extra bracket */
假设您的意思是
strncpy(dest,dev_name,sizeof(dev_name)); /* Extra bracket */
strncpy(dest,dev_name,sizeof(dev_name)); /* Extra bracket */
and dev_name
and sizeof
in you last line; strncpy(dest,dev_name,sizeof(dev_name)); /* Extra bracket */
以及最后一行中的dev_name
和sizeof
; You are assigning NUL character to the last to mark the end of name array. 您将NUL字符分配给最后一个以标记名称数组的结尾。
When you write a string literal like "foo", it is automatically NUL terminated by the compiler. 当您编写类似“ foo”的字符串文字时,编译器会自动将其以NUL终止。 When you take your own arrays, you sometimes need to mark the end of string manually.
当您使用自己的数组时,有时需要手动标记字符串的结尾。
From man strncpy
来自
man strncpy
The strncpy() function is similar, except that at most n bytes of src are copied.
除了最多复制n个src字节外,strncpy()函数与之类似。 Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated .
警告:如果src的前n个字节中没有空字节,则放置在dest中的字符串将不会以空终止 。
Explicitly the null-termination is added to handle the warning case in your code snippet. 明确地,在代码片段中添加了空终止符来处理警告情况。
dev_name[0]
, dev_name[1]
, dev_name[2]
etc are first, second, third ... characters of your string. dev_name[0]
, dev_name[1]
和dev_name[2]
等是您字符串的第一个,第二个,第三个...字符。 Assuming device name has less than 31 characters, it is automatically NUL terminated after strncpy
and you don't need to do anything. 假设设备名称少于31个字符,则在
strncpy
之后它将自动NUL终止,并且您无需执行任何操作。
If the name has exactly 31
character, last character character (32 nd ) is already '\\0'
(ascii code 0) and writing 0 again over it does not make any difference. 如果名称正好有
31
字符,则最后一个字符(32 nd )已经为'\\0'
(ASCII代码0),并在其上再次写入0不会有任何区别。
If the name has more than 31
character (corner case), last character character is not NUL and dev_name[sizeof(dev_name)-1] = 0;
如果名称包含超过
31
字符(大写),则最后一个字符不是NUL且dev_name[sizeof(dev_name)-1] = 0;
will make the name NUL terminated. 将使名称NUL终止。
In your code, assuming size
is analogous to sizeof
, 在您的代码中,假设
size
类似于sizeof
,
dev_name[size(dec_name)-1] = 0;
dev_name[size(dec_name)-1]
points to the last element of the array, remember C
arrays use 0
based indexing. dev_name[size(dec_name)-1]
指向数组的最后一个元素,请记住C
数组使用基于0
的索引。
Then, by definition, a string in c is exxentially a char
array with null-termination, so if you want to use a char array as string , you must have the null-termination. 然后,根据定义,c中的字符串是一个以 null终止的
char
数组,因此,如果要将char数组用作string ,则必须具有null终止。
0
is the ASCII value of NUL
or null
. 0
是NUL
的ASCII值或null
。 So, essentially, you're putting a null-terminator to the char
array. 因此,从本质上讲,您正在将一个空终止符放入
char
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.