I have a simple question.Why it is necessary to consider the terminating zero in an array of characters (or just a string), and not an array of integers. So when I want the string to contain 20 characters, I need to declare char string[21];. When I want to declare an array of integers containing 5 digits, then int digits[5];enough. What is the reason for this?
char string[21];
int digits[5];
You do not need to interrupt the array charwith NULLif you do not want to, but when using them to represent a string, you need to do this because C uses zero-terminated strings to represent your strings. When you use functions that work with strings (for example, strlenfor the length of a string or using printfto output a string), these functions will read the data until it is encountered NULL. If not, then you are likely to encounter buffer overflows or similar problems with access violations / segmentation.
char
NULL
strlen
printf
In short: how C represents string data.
Zero terminators are required at the end of strings (or character arrays) because:
NUL
, , ; , . , - , .
, , .
, printf %s, :
%s
int i = 0; while(input[i] != '\0') { output(input[i]); i++; }
.
, C ascii nul. ( , NULL.)
, nul nul . .
, : . "20- " 21- , .
, -, , C.
C , NUL, . "string literals" , strcpy printf .. , C , .
"string literals"
strcpy
-, , - , , , : , . C - , - Bell Labs , "" , NUL, , . ( .)
, . . . Ints c , .
, 21 . , ( ) , . .
- , C
- NUL - , ! , , . , , .
, - (, , ).
Source: https://habr.com/ru/post/1782234/More articles:Open source OCR design specifically for visualized text (screenshots) - pythonSQLite: data type depends on quotes? - sqliteHow to implement multiple relationships in SQL Server? - sqlMutual issue of passing a structure by ref, null to pointer and IntPtr - nullHow can I open Emacs on Mac OS X to boot from my $ PATH? - bashuser control in DataGridTemplateColumn - wpfhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1782236/android-ontouchevent-continuous-touch-causes-systemserver-gc&usg=ALkJrhjyh7bTJDdNsAFSGijCQwgxUY8AWABest Practices for Dual Use of iFrame + External Authentication for a Facebook-Enabled Application - facebookAsyncTask, когда он onCancelled() вызывается помимо вызова AsyncTask.cancel? - androidMultidimensional arrays in assembly language - assemblyAll Articles