c language -- detailed explanation of common string functions and sizeof

Keywords: C C++

1.sizeof use

a. Code 1

int main()
{
	int a = 0;
	int arr[] = { 1,2,3,4 };
	printf("%d\n", sizeof(a));
	printf("%d\n", sizeof a);
	printf("%d\n", sizeof(&a));//Indicates the size of the address
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(arr));//Calculates the size of the array arr in bytes
	printf("%d\n", sizeof(arr)/sizeof(arr[0]));//Calculate the number of spaces in array arr
	return 0;
}

 

Explanation:

(1)sizeof is the size of the calculation (in bytes),   sizeof can be followed by variable, type (the type must be enclosed when following the type), address (because it is a 32-bit machine, the address size is 4 bytes. If it is 64 bits, the result is 8 bytes), and array name.

(2)sizeof can also calculate the number of spaces in an array.

2.strlen function

a. Code 1

int main()
{
	char arr1[] = { 'a','b','c','\0'};
	char arr2[] = { 'a','b','c' };
	char arr3[4] = { 'a','b','c' };
	char*str = "abc";
	printf("%d\n", strlen(arr1));
	printf("%d\n", strlen(arr2));
	printf("%d\n", strlen(arr3));
	printf("%d\n", strlen(str));
	return 0;
}

 

 

Explanation:

(1)strlen function is to calculate the number of elements before '\ 0'. At this time, the arr1 array has 4 spaces, with '\ 0', and there are 3 elements before '\ 0'.

(2) At this time, the arr2 array has three spaces without '\ 0', so a random number is generated.

(3) At this time, the arr3 array has four spaces, the first three spaces are "abc", and the last space system adds a '\ 0'.

(4) The pointer variable points to the string "abc", which is equivalent to "abc"abc \ 0"".

be careful:

(1) Strlen (including StrCmp, strcpy, strcat, etc.) functions are used for strings, not integer arrays.

(1)strlen function is to calculate the number of characters before '\ 0', that is, the number of valid characters.

b. Code 2

int main()
{
	char* str1 = "abc";
	char* str2 = "abcd";
	if (strlen("abc") < strlen("abcd"))
	{
		printf("aa\n");
	}
	else
	{
		;
	}
	if (strlen(str1) - strlen(str2) > 0)
	{
		printf("aa\n");
	}
	else
	{
		;
	}
	return 0;
}

 

Explanation:

(1) The return value of strlen is an unsigned integer (unsigned int), so the final value of (strlen(str1) - strlen(str2)) is also unsigned, that is > = 0.

be careful:

(1) Be sure to index the header file < string. H >, or there will be only one "aa"

3. Comparison between sizeof and strlen functions

a. Code 1

int main()
{
	char arr1[] = { 'a','b','c' };//3 spaces, no character '\ 0'
	char arr2[] = "abc";//4 spaces, character 'c' followed by '\ 0' by default
	char arr3[] = "a\0bc";
	printf("%d\n", sizeof(arr1));
	printf("%d\n", sizeof(arr2));
	printf("%d\n", sizeof(arr3));
	printf("%d\n", strlen(arr1));
	printf("%d\n", strlen(arr2));
	printf("%d\n", strlen(arr3));
	return 0;
}

 

Explanation:

(1)sizeof is to calculate the size, and strlen is to calculate the number of characters before '\ 0'.  

4.strcmp function

a. Code 1

int main()
{
	char arr1[] = "abc";
	char arr2[] = "abc";
	char arr3[] = "bcd";
	printf("%d\n", strcmp(arr1, arr2));
	printf("%d\n", strcmp(arr1, arr3));
	return 0;
	
}

 

 

Explanation:

(1) Comparing two strings is to compare characters one by one until a mismatch is found.

be careful:

(1) When comparing two strings, strcmp returns zero if arr1 is equal to arr2. When arr1 is less than arr2,strcmp returns a number less than zero (not necessarily - 1). When arr1 is greater than arr2,strcmp returns a number greater than zero (not necessarily 1).

(2) When comparing two strings, there must be '\ 0'.

b. Code 2

int main()
{
	char arr1[] = "abc";
	char arr2[] = "abc";
	if (strcmp(arr1, arr2))
	{
		printf("aa");
	}
	else
	{
		printf("bb");
	}
	return 0;
}

 

be careful:

(1) Do not interpret the code as that arr1 is equal to arr2. If the result is true, execute the if statement. At this time, the return value of strcmp is 0, so execute the else statement.

5.strncmp function

a. Code 1

​
int main()
{
	char* str1 = "abc";
	char* str2 = "abcd";
	printf("%d\n", strncmp(str1, str2, 2));
    printf("%d\n", strncmp(str1, str2, 4));
	return 0;
}

​

 

 

  Explanation:

(1) Select the number to compare, and the return value is the same as strcmp.

int strncmp( const char *string1, const char *string2, size_t count );

6.strcpy function

a. Code 1

int main()
{
	char dest[] = "abc";
	char* src = "de";//'de 'is equivalent to' de de \ 0  '
	printf("%s", strcpy(dest, src));
	return 0;
}

 

  Explanation:

(1)

char *strcpy( char *strDestination, const char *strSource );

Copy the copy of src string (so src cannot be changed), copy it to DeST (so dest can be changed), and return the address of dest  .

(2) 'de 'string is followed by' \ 0 '. When dest is read again, the character' c 'is an invalid character.

(3) Results after copying

be careful:

(1) Array dest can be changed. It must not be a string constant, that is, char*dest="abc";

(2)src must end with '\ 0', or the program will crash.

 

(3) Ensure that dest has enough space to hold the copied string, otherwise overflow will occur.

b. Code 2

int main()
{
	char dest[4] = "abc";
	char* src = "efgh";
	printf("%s", strcpy(dest, src));
	return 0;
}

 

 

 

 

 

 

 

 

 

 

  Explanation:

(1) There are only 4 characters in dest and 5 characters copied from the past ("efgh"=="efgh"efgh" = = "efgh \ 0""). Even if the final result can be printed, the stack space around dest is destroyed.

7.strncpy function

a. Code 1

int main()
{
	char dest[] = "abcde";
	char* src = "fgh";
	printf("%s\n", strncpy(dest, src, 5));
	return 0;
}

 

Explanation:

(1)

char *strncpy( char *strDest, const char *strSource, size_t count );

The last parameter (count) of strncpy function controls the number of characters copied to dest. When count is greater than the number of src strings, array dest will be filled into count with '\ 0'.

 

 

 

b. Code 2

int main()
{
	char dest[] = "abcde";
	char* src = "fgh";
	printf("%s\n", strncpy(dest, src, 2));
	return 0;
}

Explanation:

(1) If the number of characters in SRC is greater than count, copy count characters to dest.

8.strcat function

a. Code 1

int main()
{
	char dest[7] = "abc";
	char*src = "def";
	printf("%s", strcat(dest, src));
	return 0;
}

 

  Explanation:

(1) Append the string "def" (equivalent to "def"def \ 0"") to dest.

(2) The return value is the address of the character array dest.

be careful:

(1) Ensure that dest has enough space left to accommodate the appended string, otherwise overflow will occur.

(2) The character array dest can be changed, and the string pointed to by src cannot be changed.

9.strncat function

a. Code 1

int main()
{
	char dest[7] = "abc";
	char*src = "def";
	printf("%s", strncat(dest, src,2));
	return 0;
}

 

Explanation:

(1) Append 2 characters after dest, i.e. "de"   (there is a '\ 0' after 'de' as the end flag).

(2) The return value is the address of the array dest.

be careful:

(1) Ensure that the space left in dest is enough to accommodate the additional characters, otherwise overflow will occur.

(2) The character array dest can be changed, and the string pointed to by src cannot be changed.

      The use of string function and sizeof will be shared here today. If it is helpful to you, you can pay attention to it and give it a praise.

 

 

  

 

 

 

Posted by daveyc on Fri, 05 Nov 2021 15:33:05 -0700