C language string

Posted by blufish on Fri, 18 Feb 2022 13:35:37 +0100

C language string

I character string

1. It is composed of a group of consecutive characters and is contained with "", and the last character must be '\ 0', which indicates the end of the string, and the ASCII code of '\ 0' is 0
Note: To study a string is to study the characters one by one
2. The memory space occupied by the string is continuous, and each byte stores one character
Note: the terminator of the '\ 0' string. If there are contents after it, these contents are invalid
3. Multiple parallel strings will be merged into one string by gcc in the future

for example

#include <stdio.h>

int main(void){
	//Define and initialize strings
	char a[]="helloworld\0";
	char b[10]={'1','2,'3','4','5','\0'};
	char p*="hellochina";

	printf("hello" "world"); //helloworld

	return 0;
}

1. String and pointer

a). Defining a character pointer variable and pointing to a string is essentially pointing to the first address of the string, that is, to the first address of the 0th character in the string
b). If you let a character pointer variable point to a string, this string does not need to be followed by '\ 0', gcc will help you add '\ 0' in the future
c). You cannot modify the value of the string through the character pointer variable. You can only view it, otherwise the program will crash
d). As long as you see a string, you actually see the first address of the string

for example

#include <stdio.h>

int main(void){
	//Define and initialize strings
	char a[]="hello,china";
	char *pa=a;
	printf("%s%s\n",a,pa);

	//Define and initialize string pointer variables
	char *pb="helloworld";
	//*pb="china";  // Its value cannot be modified
	printf("%s\n",pb);  //String pointer variables can only be viewed, read, and cannot be written or modified
	return 0;
}

2. String and array

#include <stdio.h>
extern int strcmp(const char *,const char *);
int main(void){
	//String writing format
	
	char a[]="helloworld";  //There is no need to add '\ 0' manually
	a="hello,china";
	printf("%s\n",a);
	
	//'\ 0' needs to be added manually
	char b[]={'h','e','l','l','o','w','o','r','l','d','\0'};
	printf("%s\n",b);
	
	char *pa="hello,world";
	char *pb="hello,china";

	//String comparison function
	int ret=strcmp(pa,pb);
	if(ret > 0){
		printf("pa greater than pb");
	}else if(0 == ret){
		printf("pa be equal to pb");
	}else if(ret < 0){
		printf("pa less than pb");
	}
	
	return 0;
}

int strcmp(const char *pa,const char *pb){
	while(*pa){
		if(*pa != *pb){
			return *pa - *pb;
		}
		pa++;
		pb++;
	}
	return *pa-pb;
}

3. String operation function

Header file to be added: #include < string h>

strlen function

Declaration prototype of strlen function: unsigned long strlen(const char *);
Function to obtain the effective length of string (excluding '\ 0')

#include <stdio.h>
#include <string.h>

int main(void){
	char a[]="helloworld":

	unsigned long int len=strlen(a);  //The calculated string does not contain a valid length of '\ 0'
	printf("Effective length of string,Not included\'\\0\':%lu %lu \n",len,strlen(a));   //10
	printf("String length,contain\'\\0\':%lu \n",sizeof(a));  //11
	
	return 0;
}

4.strcat function

Declaration prototype of strcat function: char *strcat(char *,const char *);
The function is string splicing

#include <stdio.h>
#include <string.h>

int main(void){
	//String splicing function
	char a[]="hello":
	char b[]="china":
	char *p=strcat(a,b);
	printf("%s %s\n",p,a);

	char *pa="hello";
	char *pb="world";
	char *pc=strcat(pa,pb); //Unable to write or modify the string pointer variable, and the program crashes
	return 0;
}

5.strcmp function

Declaration prototype of strcmp function: int strcmp(const char *,const char *);
Function string comparison function

#include <stdio.h>
#include <string.h>

int main(void){
	char a[]="hello,world";
	char b[]="hello,china";
	int ret=strcmp(a,b);
	
	if(ret >0 ){
		printf("a greater than b\n");
	}else if(0 == ret){
		printf("a be equal to b\n");
	}else if(ret < 0){
		printf("a less than b\n");
	}
	
	return 0;
}

6.strcpy function

Declaration prototype of strcpy function: char *strcpy(char *,const char *);
Function string copy

#include <stdio.h>
#include <string.h>

int main(void){
	char a[]="hello,world";
	char b[]="hello,china";

	char *p=strcpy(a,b);

	printf("%s %s\n",p,a);  //p=hello,china a=hello,china
	return 0;
}

7.sprintf function

Declaration prototype of sprintf function:
int sprintf(char *restrict _s, const char *restrict _format, ...);
Function to save the string composed of numbers to the array (format output function)

#include <stdio.h>
#include <string.h>

int main(void){
	char a[50];
	sprintf(a,"%s\t%d:%d:%d\n","time"12,12,12);
	printf("%s\n",a);   //time 		 12:12:12

	char b[50];
	sprintf(b,"%s:%s \n %s:%d \n %s:%s \n %s:%s \n %s:%s\n",
			"file",__FILE__,
			"Line number",__LINE__,
			"Function name",__func__,
			"date",__DATE__,
			"time",__TIME__);
	printf("%s\n",b);
	return 0;
}

II Character pointer array

1. Pointer array

1. Each element in the array is a pointer (address). The element can only be an address, not ordinary data
2. Syntax format of pointer array definition:
Data type * array name [number of elements / array length] = {address list}

3 if you need to define a large number of pointer variables in the future, it will make the code look extremely cumbersome. Use pointer array for unification, similar to array to replace a large number of variables. Now use pointer array to replace a large number of pointer variables

#include <stdio.h>

int main(void){
	int a=100;
	int b=200;
	int c=300;

	int *p1[4]={&a,&b,&c,&d};
	
	int len=sizeof(p1)/sizeof(p1[0]);

	//Read view
	for(int i=0;i<len;i++){
		printf("%d ",*(*(p1+i)));
		//printf("%d\n",*p1[i]);  // [] find the address first and then the value
	}

	//Write modification
	for(int i=0;i<len;i++){
		*(*(p1+i)) *=10;
		//*p1[i] *=10;
	}
	
	int *p2[]={&a,&b,&c};

	printf("p2 First address of:%p\n",p2);
	printf("&p2[0]First address of:%p\n",&p2[0]);
	printf("p2 Occupied memory size:%lu\n",sizeof(p2));
	printf("p2 Number of array elements:%lu\n",sizeof(p2)/sizeof(p2[0]));
	
	return 0;
}

Character pointer array

Character pointer array is a special pointer array. Each element is the first address of a string
Application scenario: a large number of string pointer variables are required!

#include <stdio.h>

int main(void){
	char *a[]={"abc","def","ghi"};
	
	printf("%s %s %s\n",*(p+0),*(p+1),*(p+2));

	int len=sizeof(a)/sizefo(a[0]);
	for(int i=0;i<len;i++){
		*(a+i) = "hellochina";
	}

	for(int i=0;i<leni++){
		printf("%s\n",*(p+i));
	}
	return 0;
}

Topics: C pointer string