Take a look at the following C ++ code snippet:
char a1[] = {'a','b','c'}; char a2[] = "abc"; cout << sizeof(a1) << endl << sizeof(a2) << endl;
Although sizeof(char) is 1 byte, why does the output show sizeof(a2) as 4, not 3 (as in the case of a1 )?
sizeof(char)
sizeof(a2)
a1
C-lines contain a null terminator, thus adding a character.
Essentially this:
char a2[] = {'a','b','c','\0'};
This is because an extra character '\0' added to the end of line C, while the first variable a1 is an array of three separate characters.
'\0'
sizeof will tell you the byte size of the variable, but prefer strlen if you want the length of the C string at runtime.
sizeof
strlen
For a2, this is a string, so it also contains '\ n'
Bugfix, following Ethan and Adam's comment, this is not "\ n", but a null terminator that is "\ 0"
Source: https://habr.com/ru/post/916549/More articles:extracting a number from a string in Java - javaHow to delay between two animations? - iosBlock all bots / crawlers / spiders for a special directory with htaccess - seoUncaught CurlException: 7: Failed to connect - facebookASP.NET Web Api - post object for custom action controller - jsonHow to wrap a ConcurrentDictionary in a BlockingCollection? - c #Is there a resource that can help convert a JavaScript prototype to jQuery? - javascriptIs it possible to always add an empty event handler? - nullDetect OS from Bash script and notify user - stringshould use float as primary key in sql server - sqlAll Articles