当前位置:网站首页>014 C language foundation: C string

014 C language foundation: C string

2022-06-27 04:24:00 Prison plan progress 50%

One : summary

stay C In language , Strings are actually empty characters \0 One dimensional character array at the end . therefore ,\0 Is used to mark the end of the string .
Null character (Null character) Also known as Terminator , abbreviation NULL, Is a value of 0 Control characters of ,\0 It's the escape character , Tell the compiler , This is not a character 0, It's an empty character .
For example, words “hello”
char greeting[6] = {'h', 'e', 'l', 'l', 'o', ''};

Because there are more at the end "", So the size of the character array is bigger than the word "hello" One more character count of .
According to the array initialization rules , You can write the above sentence as the following :char greeting[] = "hello";

 example :
#include <stdio.h>
int main(){
    
	char greeting[6] = {
    'h', 'e', 'l', 'l', 'o', '\0'};
	printf("%s \n", greeting);
	return 0;
}
 The output is :hello

 

Two : String function

function describe
strcpy(s1, s2); Copy string s2 To string s1
strcat(s1, s2); Connection string s2 To string s1 At the end of
strlen(s1); Return string s1 The length of
strcmp(s1, s2); If s1 and s2 It's the same , Then return to 0; If s1<s2 Then return less than 0; If s1>s2 Return greater than 0
strchr(s1, ch); Return a pointer , Point to string s1 In the character ch Where it first appeared .
strstr(s1, s2); Return a pointer , Point to string s1 Middle string s2 Where it first appeared .
 example :
	#include <stdio.h>
	int main(){
    
		char str1[12] = "hello";
		/*  Sure  char str1[12] = "hello";  Can not be ,char str1[12]; str1 = "hello";  This is because it defines str1 After array , Then use the equal sign to give str1 assignment , This = No. requires that the left side is a variable , The array name defined is a constant  */
		char str2[12] = "world";
		char str3[12] = "cc";
		int len;

		strcpy(str1, str3);     //  Copy str3 To str1
		printf("strcpy(str3,str1): %s \n", str3);
		/*  The result here is ccc, Why? ?  First understand str1[12]="hello"; Inside 12 It refers to the length of the array , And words hello Occupy 5 Length , Actually, it's followed by \0, All together 6 Length ,  this 6 The total length is called string length . stay C Language string , encounter \0 That is to say NULL Represents the end of the string .  that , alike str3 The string in this array is actually :"cc\0", and str1:"hello\0",  after strcpy after , Should become cc\0lo\0, first \0 Instead of l The location of . But I said before , stay C Language string , encounter \0 That is to say NULL Represents the end of the string .  therefore , The result is cc */

		strcat(str1, str2);     //  Connect str2 To str1 At the end of 
		printf("strcat(str1, str2): %s \n", str1);

		len = strlen(str1);     //  After connection ,str1 The total length of , It doesn't contain \0
		printf("strlen(str1): %d \n", len);

		int num;
		num = strcmp(str1, str2);
		printf("srcmp(str1, str2): %d \n", num);    // -1

		int ch = 'w';
		int *ptr;
		ptr = strchr(str1, ch);
		printf("%c The string after the start is  %s \n", ch, ptr);
		
		return 0;

	}
 result :
	┌──(rootkali)-[~/Desktop/c_test]
	└─# ./zifuchuan 
	strcpy(str3,str1): cc 
	strcat(str1, str2): ccworld 
	strlen(str1): 7 
	srcmp(str1, str2): -20 
	w The string after the start is  world 

strcpy The function does not check the bounds of the array , It is very easy to cause various buffer overflow vulnerabilities . These vulnerabilities are easy to exploit , And cause serious system problems . In the use of strcpy Function time , Be careful .

原网站

版权声明
本文为[Prison plan progress 50%]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270404363322.html