Skip to main content
deleted 9 characters in body
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358
char s[12]; strcpy( s, "Hello" ); strcpy( s + sizeof( "Hello" ), "World" ); puts( s ); // outputs is "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" 
char *s = ( char * )t; puts( s ); // outputs is "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" 
char s[] = "Hello World"; printf( "%zu\n", sizeof( s ) ); // outputs 12 strtok( s, " " ); puts( s ); // outputs is "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" printf( "%zu\n", sizeof( s ) ); // outputs 12 
char s[12]; strcpy( s, "Hello" ); strcpy( s + sizeof( "Hello" ), "World" ); puts( s ); // outputs is "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" 
char *s = ( char * )t; puts( s ); // outputs is "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" 
char s[] = "Hello World"; printf( "%zu\n", sizeof( s ) ); // outputs 12 strtok( s, " " ); puts( s ); // outputs is "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" printf( "%zu\n", sizeof( s ) ); // outputs 12 
char s[12]; strcpy( s, "Hello" ); strcpy( s + sizeof( "Hello" ), "World" ); puts( s ); // outputs "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" 
char *s = ( char * )t; puts( s ); // outputs "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" 
char s[] = "Hello World"; printf( "%zu\n", sizeof( s ) ); // outputs 12 strtok( s, " " ); puts( s ); // outputs "Hello" puts( s + sizeof( "Hello" ) ); // outputs "World" printf( "%zu\n", sizeof( s ) ); // outputs 12 
deleted 1 character in body
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

If you wrote "Hello\0Hi" then the compiler may not itself just remove this part Hi from the literal. It havehas to store it in memory along with other characters of the literal enclosed in quotes.

Standard C function strlen is specially written for character arrays that to find the length of a stored sttringstring in a character array. It does not know what data are stored in an array and how they were written in it. All what it does is searches the first zero character in a character array and returns the number of characters in the character array before the zero character.

If you wrote "Hello\0Hi" then the compiler may not itself just remove this part Hi from the literal. It have to store it in memory along with other characters of the literal enclosed in quotes.

Standard C function strlen is specially written for character arrays that to find the length of a stored sttring in a character array. It does not know what data are stored in an array and how they were written in it. All what it does is searches the first zero character in a character array and returns the number of characters in the character array before the zero character.

If you wrote "Hello\0Hi" then the compiler may not itself just remove this part Hi from the literal. It has to store it in memory along with other characters of the literal enclosed in quotes.

Standard C function strlen is specially written for character arrays that to find the length of a stored string in a character array. It does not know what data are stored in an array and how they were written in it. All what it does is searches the first zero character in a character array and returns the number of characters in the character array before the zero character.

added 39 characters in body
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

Another example. Standard C function strtok can split one string stored in a character array to several strings substituting the specified by the user delimiters with zero bytes. As result the character arryarray will contain several strings.

The last printf statement will output the same value equal to 12 because the array occupies the same number of bytes. Simply one byte in the memory allocated for the array was changed from ' ' to '\0'.

Another example. Standard C function strtok can split one string stored in a character array to several strings substituting the specified by the user delimiters with zero bytes. As result the character arry will contain several strings.

The last printf statement will output the same value equal to 12 because the array occupies the same number of bytes. Simply one byte was changed from ' ' to '\0'.

Another example. Standard C function strtok can split one string stored in a character array to several strings substituting the specified by the user delimiters with zero bytes. As result the character array will contain several strings.

The last printf statement will output the same value equal to 12 because the array occupies the same number of bytes. Simply one byte in the memory allocated for the array was changed from ' ' to '\0'.

Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358
Loading