Definition of strings and when to add '\0'

Six forms of strings are defined:

①、 char *arr="hello world"; 

  Rationale: The value of a string constant is essentially the address of the first character.

②、 char *arr;arr="hello world"; 

  A rewrite of ①.

③、 char arr[]="hello";

  The character array is used to store the string. The actual length of the string in memory is 6 chars, including '\0'.

  1 printf("%d\n",strlen(arr));//5, the length calculated by strlen does not include '\0', and ends the calculation at the first '\0', such as for the string "abc \0def", the calculation result of strlen is 3 
  2 printf("%d\n",sizeof(arr)/sizeof(arr[0]));//6, the length calculated by sizeof contains '\0' 
  3 printf ("%d\n",sizeof(arr)/sizeof(char));/,6, has the same meaning as the previous sizeof, it is a rewrite

④、char arr[]={'H','e','l','l','o','\0'}; 

  If you do not write '\0' at the end, it is an ordinary character array and does not constitute a string.

 ⑤、 char arr[6]="Hello"; 

  When defining a fixed-length array, the length given should be at least one bit larger than the actual length of the string, leaving a place for '\0'. In this example, the <5 array overflows and fails to compile, and =5 does not leave a place for '\0', which is an ordinary character array and does not constitute a string.

  Strings are required to end with a '\0' as a closing marker, but character arrays are not required to contain '\0'. However, it should be noted that when defining a fixed-length array, if the fixed length is greater than the actual length of the string, the empty part will be automatically filled with '\0'. Such as char arr[5]="ab"; , the actual is like this:'a','b','\0','\0','\0'

  1 char arr[10]="hello";
  2 printf("%d\n",strlen(arr));//5
  3 printf("%d\n",sizeof(arr)/sizeof(arr[0]));//10
  4 printf("%d\n",sizeof(arr)/sizeof(char));//10

⑥、char arr[6]={'H','e','l','l','o'};

  When defining a fixed-length array, the length given should be at least one digit larger than the actual length of the string. Leave a place for '\0', and '\0' will be automatically added. See ⑤ for details.

Commonly mistaken definition methods:

  •  char a[6]; a[6]="Hello"; 
  •   Error reason: a[6] is a char variable, "Hello" is essentially the address of the first character and cannot be assigned a value.
  •  char a[6]; a="Hello";  char a[]; a="Hello";  
  •   Reason for the error: Although the array name a is a pointer, it has already pointed to the 6 char length space allocated for a[6] in the memory, and now it points to the address of "Hello", the pointer will be confused. Note the difference with ②.

Notice:

There is a difference between defining a string as a character array and defining a string as a pointer:

1. Using the character array form, a copy is copied from the string constant word. When modifying the content of the character array, only the copy is modified, and the string in the constant word will not be affected;

②, using the pointer form will affect the string in the constant word.

 

Guess you like

Origin blog.csdn.net/weixin_55305220/article/details/122194814