简体   繁体   English

array [index] = 0;的目的是什么?

[英]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_namesizeof 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 . 0NULASCII值null So, essentially, you're putting a null-terminator to the char array. 因此,从本质上讲,您正在将一个空终止符放入char数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 限制为数组大小的目的是什么? - What is the purpose of restrict as size of array? 在c中使用数组进行LIFO内存分配的目的是什么? - What is the purpose of LIFO memory allocation with an array in c? 在函数中将数组声明为静态的目的是什么? - What is the purpose of declaring the array as static in the function? 2D数组的三重指针的目的是什么? - What is the purpose of a triple pointer for 2d array? 具有未定义长度但只有一个int元素的数组的目的是什么? - What is the purpose of an array with undefined length but only one int element? “volatile”关键字出现在数组下标中的目的是什么? - What is the purpose of the “volatile” keyword appearing inside an array subscript? void * array = *(void **)member + siz *(* p_n)的目的是什么? - What is the purpose of void *array = *(void **) member + siz * (*p_n); 在回文分区问题中,这个 DP 数组的基本情况 dp[0] = -1 的目的是什么? - What is the purpose of the base case dp[0] = -1 of this DP array in the palindromic partitions problem? 大小数组的目的是什么,因为 function 参数是 c 和 c++? - What is the purpose of sized array as function argument is c and c++? 将b声明为指向int数组10的指针的目的是什么? - what is the purpose of Declaring b as pointer to array 10 of int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM