The Road to C Language Learning (Basics) - Pointers (Part 2)

Note: This blog is written word by word by the blogger, which is not easy. Please respect the originality, thank you all!

pointers and functions

1) The function parameter changes the value of the actual parameter

Example 1:Integer variables are used as formal parameters of functions and cannot change the value of the actual parameters

#include <stdio.h>

void swap(int a, int b) {
    
    

	int c;
	c = a;
	a = b;
	b = c;
	printf("a=%d, b=%d\n", a, b);
	return;
}

int main() {
    
    

	int a = 10;
	int b = 20;
	swap(a, b);
	printf("a=%d, b=%d\n", a, b);


	return 0;
}
输出结果
a=20, b=10
a=10, b=20

Example 2:Pointer variables serve as formal parameters of functions and can change the values ​​of actual parameters

#include <stdio.h>

void swap(int a, int b) {
    
    

	int c;
	c = a;
	a = b;
	b = c;
	printf("a=%d, b=%d\n", a, b);
	return;
}
void swap2(int *x, int *y) {
    
    
	
	int c = *x;
	*x = *y;
	*y = c;
	printf("c=%d x=%d, y=%d\n", c, *x, *y);
	return;
}
int main() {
    
    

	int a = 10;
	int b = 20;
	//swap(a, b);
	swap2(&a, &b);
	printf("a=%d, b=%d\n", a, b);

	return 0;
}

Insert image description here

输出结果
c=10 x=20, y=10
a=20, b=10

2) Array name as function parameter

The array name is used as a function parameter, and the formal parameter of the function will degenerate into a pointer.

#include <stdio.h>

// 数组作为函数的形参会退化为指针
// 形参int b[10]就退化成int *b,哪怕形参是int b[10000],也是int *b
void printf_arr(int b[10]) {
    
    

	// 这里sizeof(b)==sizeof(int)==4个字节
	// sizeof(b[0])其中b[0] == int *(b+0) == int *b == a == 3,所以sizeof(b[0]) == sizeof(3) == sizeof(int)==4个字节
	int len = sizeof(b) / sizeof(b[0]); // 4/4=1
	for (int i = 0; i < len; i++)
	{
    
    
		printf("%d ", b[i]); // 3
	}


	return 0;
}

int main() {
    
    

	int a[10] = {
    
     3,9,5,1,4,7,6,10,2,8 };
	printf_arr(a); // 实参a为数组名,也就是首元素地址 &a[0], 类型为int*类型

	return 0;
}

Then you need to pass the number of array elements and let the printf_arr function know it before you can print other elements of the array.

#include <stdio.h>

// 数组作为函数的形参会退化为指针
// 形参int b[10]就退化成int *b,哪怕形参是int b[10000],也是int *b
void printf_arr(int b[10], int len) {
    
    

	// 这里sizeof(b)==sizeof(int)==4个字节
	// sizeof(b[0])其中b[0] == int *(b+0) == int *b == a == 3,所以sizeof(b[0]) == sizeof(3) == sizeof(int)==4个字节
	//int len = sizeof(b) / sizeof(b[0]); // 4/4=1
	for (int i = 0; i < len; i++)
	{
    
    
		printf("%d ", b[i]); // 3
	}


	return 0;
}

int main() {
    
    

	int a[10] = {
    
     3,9,5,1,4,7,6,10,2,8 };
	int len = sizeof(a) / sizeof(a[0]);
	printf_arr(a, len); // 实参a为数组名,也就是首元素地址 &a[0], 类型为int*类型

	return 0;
}

Insert image description here

3) Pointer as return value of function

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int* getNum() {
    
    
	// srand()用来设置rand()产生随机数时的随机种子
	srand(time(0));
	int num = rand();
	return &num;

}

int main() {
    
    

	int* p = getNum();

	printf("%d\n", *p);


	return 0;
}

Insert image description here

Supplement:Variables defined in a function are called local variables. Local variables will be released once the function ends, so after the getNum function returns, go to the main function It is illegal to operate on this freed space, but the compiler will not tell us that this space is not occupied, so there will be no problem, but if it is occupied, an error will occur.

Variables defined outside the function are called global variables and can be used by the entire project; the entire variable program starts to open up space, and the space is not released until the end of the program.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 在函数外面定义的变量叫全局变量,整个工程都可以使用
// 整个变量程序启动开辟空间,直到程序结束释放空间
int num = 0;

int* getNum() {
    
    
	// srand()用来设置rand()产生随机数时的随机种子
	srand(time(0));
	//{}中定义的变量叫局部变量,局部变量一旦在函数结束之后就会被释放空间
	//int num = rand();
	num = rand();
	return &num;

}

int main() {
    
    

	int* p = getNum();

	printf("%d\n", *p);


	return 0;
}

Insert image description here

pointers and strings

1) Character pointer

Example 1:

#include <stdio.h>

int main() {
    
    
	//指针与字符串
	char str[] = "cdtaogang"; //定义了一个字符数组,字符数组内容为cdtaogang\0
	//定义一个指针用来保存数组首元素的地址
	char* p = str;
	printf("%s\n", p); //%s打印一个字符串,要的是首个字符的地址
	printf("%s\n", p+2);
	printf("%c\n", *(p + 3));
	printf("%d\n", sizeof(str));
	printf("%d\n", sizeof(p));

	return 0;
}
输出结果
cdtaogang
taogang
a
10
4

Example 2:

#include <stdio.h>

int main() {
    
    
	//指针与字符串
	char str[] = "cdtaogang"; //定义了一个字符数组,字符数组内容为cdtaogang\0
	//定义一个指针用来保存数组首元素的地址
	char* p = str;
	*p = 'N';
	p++;
	*p = 'B';
	p++;
	printf("%s\n", p);
	printf("%s\n", p-2);

	return 0;
}
输出结果
taogang
NBtaogang

2) String constant

  • String constants cannot be changed and exist in the literal constant area.
  • The contents of the literal constant area cannot be changed.
  • char *p = "hello";Represents assigning the address of a string constant to a pointerp

Example:

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

int main() {
    
    

    char  str[] = "helloworld";//定义了一个字符数组,字符数组内容为helloworld
                                                      //定义一个指针用来保存数组首元素的地址
    char* p = str;
    p = "abcdef";//字符串常量存文字常量区,""在使用时,取的是字符串首元素的地址
    printf("%s\n", p); // abcdef
    printf("%d\n", sizeof(p));//4
    printf("%d\n", sizeof("abcdef"));//7   abcdef\0
    printf("%d\n", strlen(p));//6
    printf("%d\n", strlen("abcdef"));//6  strlen返回字符串长度
    //文字常量区的内容是不可以改变的
    p = 'm';
    printf("%s\n", p); 

	return 0;
}
输出结果
abcdef
4
7
6
6

3) Character pointer as function parameter

Example 1:

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

void mystrcat(char* dst, char* src)
{
    
    
	int n = strlen(dst);
	int i = 0;
	while (*(src + i)!=0)
	{
    
    
		*(dst+n) = *(src + i);
		i++;
		n++;
	}
	*(dst + n) = 0;
}

int main()
{
    
    
	char str1[128] = "hello ";
	char str2[128] = "cdtaogang";

	mystrcat(str1, str2); // 不用传递元素个数,因为字符数组最后一个元素为\0,可以以此作为判断
	printf("%d\n", strlen(str1));
	printf("%d\n", sizeof(str1));
	printf("str1 = %s", str1); 

	return 0;
}
输出结果
15
128
str1 = hello cdtaogang

Example 2:

#include <stdio.h>

void mystrcat(char* dst, const char* src)
{
    
    
	int len1 = 0;
	int len2 = 0;
	// 遍历数组计算元素个数
	while (dst[len1])
	{
    
    
		len1++;
	}
	while (src[len2])
	{
    
    
		len2++;
	}

	int i;
	for (i = 0; i < len2; i++)
	{
    
    
		dst[len1 + i] = src[i];
	}
}

int main()
{
    
    
	char str1[100] = "hello ";
	char str2[] = "cdtaogang";

	mystrcat(str1, str2);
	printf("str1 = %s\n", str1);

	return 0;
}
输出结果
str1 = hello cdtaogang

4) const modified pointer variable

  • constModify a variable to be read-only
  • constModify the pointer. The content of the space pointed by the pointer cannot be modified through the pointer.
  • constModify the pointer variable. After initialization, the pointer of the pointer variable itself cannot be changed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    
    
	//const修饰一个变量为只读
	const int a = 10;
	//a = 100; //err

	//指针变量, 指针指向的内存, 2个不同概念
	char buf[] = "cdtaogang";

	//从左往右看,跳过类型,看修饰哪个字符
	//如果是*, 说明指针指向的内存不能改变
	//如果是指针变量,说明指针的指向不能改变,指针的值不能修改
	
	// const修饰指针,不能通过指针修改指针所指向的空间内容,但p变量本身的值是可以被修改的
	const char* p = buf; 
	// 等价于上面 char const *p1 = buf;
	//p[1] = '2'; //err // 不能通过p指针修改那块空间的内容
	p = "agdlsjaglkdsajgl"; //ok

	// const修饰指针变量p2初始化之后不能改变p2指针变量本身的指向
	char* const p2 = buf;
	p2[1] = '3';
	//p2 = "salkjgldsjaglk"; //err // 不能改变p2指针变量本身的指向

	// const修饰指针和指针变量,p3为只读,指向不能变,指向的内存也不能变
	const char* const p3 = buf;

	return 0;
}

5) Character pointer array *

Character pointer array: It is an array, each element is a character pointer.

Example 1:

#include <stdio.h>
//字符指针数组
int main() {
    
    
	char* p1 = "hello";
	char* p2 = "hi";
	char* p3 = "hey";
	//char* str[3] = { p1, p2,p3 };
	char* str[3] = {
    
     "hello", "hi","hey" };
	for (int i = 0; i < 3; i++)
	{
    
    	
		printf("%s ", str[i]);
	}
	printf("\n");
	// 打印首元素的值
	printf("%c\n", *str[0]);
	// 打印字符串hi中的i
	printf("%c\n", *(str[1] + 1));
	// 打印字符串hey中的y
	printf("%c\n", *(str[2] + 2));
	// 打印字符指针数组所有元素中的e字符
	for (int i = 0; i < sizeof(str); i++)
	{
    
    	
		for (int j = 0; j < strlen(str[i]); j++)
		{
    
    
			if (str[i][j] == 'e')
			{
    
    
				printf("%c\n", str[i][j]);
			}
		}
	}
	return 0;
}

Insert image description here

输出结果
hello hi hey
h
i
y
e
e

Example 2:

#include <stdio.h>

int main() {
    
    

	char* str[3] = {
    
     "hello", "hi","hey" };
	//定义一个指针保存str数组首元素的地址
	char** p = str;  // &str[0] == str

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

	return 0;
}

Insert image description here

输出结果
hellohello
hihi
heyhey

Example 3:

#include <stdio.h>

int main() {
    
    

	char* str[3] = {
    
     "hello", "hi","hey" };
	//定义一个指针保存str数组首元素的地址
	char** p = str;  // &str[0] == str
	
	// 通过指针p取出首元素的值
	printf("%c", *(*(p + 0))); // *(p+0)得到str[0]里面的内容0x1000,而0x1000又代表hello\0首元素地址,所以取首元素的值就是*(*(p+0))
	printf("%c", *(p[0]+0)); // 上一步简写
	printf("%c", p[0][0]); // 再简写
	// 通过指针p取出数组元素hey中的e字符
	printf("%c", *(*(p + 2) + 1)); // *(p+2)得到str[2]里面的内容0x3000,而0x3000又代表hey\0首元素地址,在首元素地址上+1即*(p+2)+1就得到e字符的地址,所以取e字符就是*(*(p+2)+1)
	printf("%c", *(p[2] + 1));
	printf("%c", p[2][1]);
	return 0;
}

Insert image description here

6) Pointer array as formal parameter of main function

int main(int argc, char *argv[]);
  • mainThe function is called by the operating system. The first parameter indicatesargcthe number of members of the array. argvEach member of the array ischar *Type
  • argvIt is a character pointer array of command line parameters, which stores the address of the first element of the parameter (string).
  • argcRepresents the number of command line parameters. The program name itself counts as one parameter.
#include <stdio.h>

//argc: 传参数的个数(包含可执行程序)
//argv:指针数组,指向输入的参数
// .*.exe   hello  123456
int main(int argc, char* argv[])
{
    
    
	// 打印运行参数
	printf("%d\n", argc);
	printf("%s\n", argv[0]);
	printf("%s\n", argv[1]);
	printf("%s\n", argv[2]);
	printf("%s\n", argv[3]);
	return 0;
}

Right-click the project>Properties and configure debugging command parametershello 123456

Insert image description here

is equivalent tochar* argv[] = {".*.exe", "hello", "123456"}. If the parameters are not enough, the printed value isnull

Insert image description here

The number of passed parameters can be known through the value ofargc, so the input parameters can be printed through the for loop; it should be noted that There is.*.exeprogram path before the command line parameters.

#include <stdio.h>

//argc: 传参数的个数(包含可执行程序)
//argv:指针数组,指向输入的参数
// .*.exe   hello  123456
// char* argv[] = {".*.exe", "hello", "123456"}
int main(int argc, char* argv[])
{
    
    
	for (int i = 0; i <argc; i++)
	{
    
    
		printf("%s\n", argv[i]);
	}
	return 0;
}
输出结果
E:\VisualStudioProjects\StudyCProject\Debug\StudyCProject.exe
hello
123456

We can use thegcc command to compile and generateexe an executable program to print command line parameters

#include <stdio.h>
// 形参名随便取
int main(int n, char *v[]){
    
    
	
	for(int i = 0; i < n; i++)
	{
    
    
		printf("%s\n", v[i]);
	}
	
	return 0;
}
PS C:\Users\Administrator\Desktop> gcc hello.c -o hello
PS C:\Users\Administrator\Desktop> ./hello.exe hello CSDN cdtaogang good good study

Insert image description here

7) String processing function

7.1 strcpy()

Copy string:

  • Header file:#include <string.h>
  • Define function:char *strcpy(char *dest, const char *src);
  • Function: Copy the string pointed to bysrc to the space pointed by dest, '\0' will also Copy over
  • Parameters:
    dest:First address of destination string
    src:First address of source character
  • Return value:
    Success: returndestThe first address of the string
    Failure:NULL

Note:If the memory space pointed to by the parameter dest is not large enough, a buffer overflow error may occur.

Example 1:

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

int main() {
    
    
	// strcpy函数
	char str1[128] = "";
	char str2[128] = "cdtaogang";
	char str3[128] = "hello\0cdtaogang";
	char str4[128] = "csdn";
	strcpy(str1, str2);
	printf("%s\n", str1); // cdtaogang
	strcpy(str1, str3); // 遇到 '\0'就结束拷贝
	printf("%s\n", str1); // hello
	strcpy(str2, str4); // csdn后面的'\0'也会拷贝过去,遇到'\0'结束掉相当于替换掉原始数据
	printf("%s\n", str2); // csdn
	return 0;
}
输出结果
cdtaogang
hello
csdn

7.2 strncpy()

Copy string:

  • Header file:#include <string.h>
  • Define function:char *strncpy(char *dest, const char *src, size_t n);
  • Function: Copy the first characters of the string pointed to bysrc to the space pointed by. Whether to copy the terminator to see if the specified length contains . ndest'\0'
  • Parameters:
    dest:The first address of the destination string
    src:The first address of the source character
    n:Specify the number of strings to be copied
  • Return value:
    Success: returndestThe first address of the string
    Failure:NULL

Example 1:

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

int main() {
    
    
	// strncpy函数
	char str1[128] = "";
	char str2[128] = "hellocdtaogang";
	char str3[128] = "he\0llocdtaogang";

	strncpy(str1, str2, 2);
	printf("str1=%s\n", str1); // he

	strncpy(str1, str2, 5);
	printf("str1=%s\n", str1); // hello

	strncpy(str1, str3, 5);
	printf("str1=%s\n", str1); // he
	// 验证将将str3字符串前5个字符拷贝至str1中是 he\0\0\0 还是 he\0wl
	for (int i = 0; i < 5; i++)
	{
    
    
		printf("%d ", str1[i]); // 104 101 0 0 0 说明拷贝的情况是不足n个字符就遇到了\0那么后面就不拷贝了
	}
	return 0;
}
输出结果
str1=he
str1=hello
str1=he
104 101 0 0 0

7.3 strcat()

Concatenate two strings:

  • Header file:#include <string.h>
  • Define function:char *strcat(char *dest, const char *src);
  • Function: Connectsrc string to the end of dest, ‘\0’ will also be appended
  • Parameters:
    dest:First address of destination string
    src:First address of source character
  • Return value:
    Success: returndestThe first address of the string
    Failure:NULL

Example 1:

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

int main() {
    
    
   // strcat函数
   char str1[128] = "hello";
   char str2[128] = "cdtaogang";
   char str3[128] = "world\0ocdtaogang";

   strcat(str1, str2);
   printf("str1=%s\n", str1); // hellocdtaogang

   strcat(str1, str3);
   printf("str1=%s\n", str1); // hellocdtaogangworld

   return 0;
}
输出结果
str1=hellocdtaogang
str1=hellocdtaogangworld

7.4 strncat()

Concatenate two strings:

  • Header file:#include <string.h>
  • Define function:char *strncat(char *dest, const char *src, size_t n);
  • Function: Connect the first characters of the src string to the end of , < a i=4>will also be appendedndest‘\0’
  • Parameters:
    dest:The first address of the destination string
    src:The first address of the source character
    n:Specifies the number of strings to be appended
  • Return value:
    Success: returndestThe first address of the string
    Failure:NULL

Example 1:

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

int main() {
    
    
	// strncat函数
	char str1[128] = "hello";
	char str2[128] = "123456";
	char str3[128] = "world\0ocdtaogang";

	strncat(str1, str2, 3);
	printf("str1=%s\n", str1); // hello123

	strncat(str1, str3, 10);
	printf("str1=%s\n", str1); // hello123world

	return 0;
}
输出结果
str1=hello123
str1=hello123world

7.5 strcmp()

Compare strings:

  • Header file:#include <string.h>
  • Define function:int strcmp(const char *s1, const char *s2);
  • Function: Compare the sizes of s1 and s2. What is compared is the character ASCII code size.
  • Parameters:
    s1:String 1 first address
    s2:String 2 first address
  • Return value:
    Equal:0
    Greater than:>0 On different operating systemsstrcmpThe results will be returned differentlyASCIIThe difference
    is less than:<0

Example 1:

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

int main() {
    
    
	// strcmp函数
	char* str1 = "hello world";
	char* str2 = "hello cdtaogang";
	char* str3 = "hello cdtaogang";
	char* str4 = "zzzzzzzzz";

	printf("strcmp(str1, str2):%d\n", strcmp(str1, str2)); // 1
	printf("strcmp(str2, str3):%d\n", strcmp(str2, str3)); // 0
	printf("strcmp(str2, str4):%d\n", strcmp(str2, str4)); // -1

	if (strcmp(str1, str2) == 0)
	{
    
    
		printf("str1==str2\n");
	}
	else if (strcmp(str1, str2) > 0)
	{
    
    
		printf("str1>str2\n");
	}
	else  // < 0
	{
    
    
		printf("str1<str2\n");
	}

	return 0;
}
输出结果
strcmp(str1, str2):1
strcmp(str2, str3):0
strcmp(str2, str4):-1
str1>str2

7.6 strncmp()

Compare strings:

  • Header file:#include <string.h>
  • Define function:int strncmp(const char *s1, const char *s2, size_t n);
  • Function: Compare the size of the first characters of s1 and s2, and compare the characters code size. nASCII
  • Parameters:
    s1:The first address of string 1
    s2:The first address of string 2
    n:Specify the number of comparison strings
  • Reply:
    Equal:0
    Dayu:>0
    Xiaoyu:<0

Example 1:

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

int main() {
    
    
	// strncmp函数
	char* str1 = "hello world";
	char* str2 = "hello cdtaogang";
	char* str3 = "hello cdtaogang";
	char* str4 = "zzzzzzzzz";

	printf("strncmp(str1, str2, 7):%d\n", strncmp(str1, str2, 7)); // 1
	printf("strncmp(str2, str3, 6):%d\n", strncmp(str2, str3, 6)); // 0
	printf("strncmp(str2, str4, 1):%d\n", strncmp(str2, str4, 1)); // -1

	if (strncmp(str1, str2, 8) == 0)
	{
    
    
		printf("str1==str2\n");
	}
	else if (strncmp(str1, str2, 8) > 0)
	{
    
    
		printf("str1>str2\n");
	}
	else  // < 0
	{
    
    
		printf("str1<str2\n");
	}

	return 0;
}
输出结果
strncmp(str1, str2, 7):1
strncmp(str2, str3, 6):0
strncmp(str2, str4, 1):-1
str1>str2

7.7 sprintf()

Formatted string copy (group package function)

  • Header file:#include <stdio.h>
  • Define function:int sprintf(char *str, const char *format, ...);
  • Function: Convert and format the data according to the parameter format string, and then output the result to the space specified by str until the string appears end character'\0'.
  • Parameters:
    str:String first address
    format:String format, usage is the same asprintf()
  • Return value:
    Success: The actual number of characters formatted
    Failure: -1

Example 1:

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

int main() {
    
    
	// sprintf函数
	int year = 2022;
	int month = 11;
	int day = 19;

	//printf("today is %d-%d-%d\n", year, month, day);

	char buf[1024] = "";
	char buf2[1024] = "";
	sprintf(buf, "today is %d-%d-%d\n", year, month, day); // 将数据安装格式组包,存放在数组buf中,sprintf函数的返回值是组完包的有效长度
	printf("buf=[%s]\n", buf); // buf=[today is 2022-11-19
                               //]
	// 打印buf字符个数
	int len2 = sprintf(buf2, "to%cday is %d-%d-%d\n", 0, year, month, day);
	printf("%d\n", strlen(buf2)); // 2  strlen遇到0就结束了,所以字符个数不准确
	printf("len=%d\n", len2); // len=21
	return 0;
}
输出结果
buf=[today is 2022-11-19
]
2
len=21

7.8 sscanf()

Formatted string input (unpacking function)

  • Header file:#include <stdio.h>
  • Define function:int sscanf(const char *str, const char *format, ...);
  • Function: Read data from the string specified by str, and convert and format the data according to the parameter format string.
  • Parameter:
    str:The specified first address of the string
    format:String format, usage is the same asscanf()
  • Return value:
    Success: number of parameters, number of successfully converted values
    Failure: -1

Example 1:

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

int main() {
    
    
	// sscanf函数
	int year;
	int month;
	int day;

	//scanf("%d-%d-%d", &year, &month, &day);// 从键盘按照相应的格式获取数据
	char buf[1024] = "beijing:2022-11-19";
	sscanf(buf, "beijing:%d-%d-%d", &year, &month, &day); // 从buf中按照相应的格式获取数据
	printf("today is %d-%d-%d", year, month, day);

	return 0;
}
输出结果
today is 2022-11-19

7.9 strchr()

Find the first occurrence of a specified character in a string

  • Header file:#include <string.h>
  • Define function:char *strchr(const char *s, int c);
  • Function: Find the position where letters appear in the stringsc
  • Parameters:
    s:String first address
    c:Match letters (characters)
  • Return value:
    Success: Return the first occurrence ofcaddress
    Failure:NULL

Example 1:

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

int main() {
    
    
	// 编写代码实现第一次出现a字符的地址
	char src[] = "ddda123abcd";
	int i = 0;
	char* p = src;
	while (p[i]!=0)
	{
    
    
		if (p[i] == 'a')
		{
    
    
			p = &p[i];
			break;
		}
		i++;
	}
	printf("p = %s\n", p);

	return 0;
}

Example 2:

char *my_strchr(char* p, char ch) {
    
    

	int i = 0;
	while (p[i]!=0)
	{
    
    
		if (p[i]==ch)
		{
    
    
			return &p[i];
		}
		i++;
	}
	return NULL;
}
int main() {
    
    
	// 定义函数实现
	char src[] = "ddda123abcd";
	char* p = my_strchr(src, 'a');
	printf("p = %s\n", p);

	return 0;
}

Example 3:

int main() {
    
    
	// strchr函数
	char src[] = "ddda123abcd";
	char* p = strchr(src, 'a');
	printf("p = %s\n", p);

	return 0;
}

Insert image description here

7.10 strstr()

Find a specified string in a string

  • Header file:#include <string.h>
  • Define function:char *strstr(const char *haystack, const char *needle);
  • Function: Find the position where string appears in stringhaystackneedle
  • Parameters:
    haystack:First address of source string
    needle:First address of matching string
  • Return value:
    Success: Return the first occurrence ofneedleaddress
    Failure:NULL

Example 1:

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

char* my_strstr(char* str1, char *str2) {
    
    

	int i = 0;
	// str[i] == *(str+i)
	while (str1[i] != 0)
	{
    
    
		if (str1[i] == str2[0])
		{
    
    	// str1+i就表示str1中'a'的地址
			if (0==strncmp(str1+i, str2, strlen(str2)))
			{
    
    
				return str1+i;
			}
		}
		i++;
	}
	return NULL;
}
int main() {
    
    
	// 定义函数实现
	char str1[] = "ddddabcd123abcd333abcd";
	char str2[] = "abcd";
	char* p = my_strstr(str1, str2);
	printf("p = %s\n", p);
	return 0;
}

Example 2:

int main() {
    
    
	// strstr函数
	char str1[] = "ddddabcd123abcd333abcd";
	char str2[] = "abcd";
	char* p = strstr(str1, str2);
	printf("p = %s\n", p);
	return 0;
}
输出结果
p = abcd123abcd333abcd

7.11 strtok()

split string

  • Header file:#include <string.h>
  • Define function:char *strtok(char *str, const char *delim);
  • Function: To split the string into fragments. When strtok() finds the dividing character contained in parameter in the string of parameter s, the character will be changed to characters, when multiple characters appear in succession, only the first one is replaced with . delim\0\0
  • Parameters:
    str:points to the string to be split
    delim:points to all characters contained in the split string
  • Return value:
    Success: first address of the string after splitting
    Failure:NULL

Example 1:

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

int main() {
    
    
	// strtok函数
	char str[] = "CSDN#1126331350#cdtaogang#9528";
	char* p1 = strtok(str, "#");
	printf("p1=%s\n", p1); // p1=CSDN
	// strtok会记录每次切割,并将切割部分内部记录为\0,所以第二次及之后的切割从NULL开始往后找
	char* p2 = strtok(NULL, "#");
	printf("p2=%s\n", p2); // p2=1126331350
	char* p3 = strtok(NULL, "#");
	printf("p3=%s\n", p3); // p3=cdtaogang
	char* p4 = strtok(NULL, "#");
	printf("p4=%s\n", p4); // p4=9528
	
	return 0;
}

Example 2:

int main() {
    
    
	// strtok函数
	char str[] = "CSDN#1126331350#cdtaogang#9528";
	// 使用while循环将"#"分割的子串全部取出
	char* s = strtok(str, "#");
	while (s != NULL)
	{
    
    
		printf("%s\n", s);
		s = strtok(NULL, "#");
	}
	return 0;
}

Example 3:

int main() {
    
    
	// strtok函数
	char str[] = "CSDN#1126331350#cdtaogang#9528";
	// 使用do while循环将"#"分割的子串全部取出
	char* p[10] = {
    
     NULL };
	int i = 0;
	do
	{
    
    
		if (i == 0) // 表示第一次切割
		{
    
    
			p[i] = strtok(str, "#");
		}
		else
		{
    
    
			p[i] = strtok(NULL, "#");
		}
	} while (p[i++]!=NULL); // i在前先使用再++,即p[0]!=NULL i=i+1 如果strtok的返回值等于NULL,代表切割完毕
	// 循环打印
	i = 0;
	while (p[i++]!=NULL)
	{
    
    
		printf("%s\n", p[i-1]); // 打印时i已经+1了

	}
	return 0;
}

Example 4:

int main() {
    
    
	// strtok函数
	char str[] = "CSDN#1126&331350#cdtaogang#95&28";
	// 使用do while循环将"#&"分割的子串全部取出
	char* p[10] = {
    
     NULL };
	int i = 0;
	do
	{
    
    
		if (i == 0) // 表示第一次切割
		{
    
    
			p[i] = strtok(str, "#&");
		}
		else
		{
    
    
			p[i] = strtok(NULL, "#&");
		}
	} while (p[i++]!=NULL); // i在前先使用再++,即p[0]!=NULL i=i+1 如果strtok的返回值等于NULL,代表切割完毕
	// 循环打印
	i = 0;
	while (p[i++]!=NULL)
	{
    
    
		printf("%s\n", p[i-1]); // 打印时i已经+1了
	}
	return 0;
}

Insert image description here

7.12 trailer()

Convert string to integer

  • Header file:#include <stdlib.h>
  • Define function:int atoi(const char *nptr);
  • Function:atoi() will scannptr the string, skip the preceding space characters, and do not start conversion until it encounters numbers or signs. When encountering a non-number or string end character('\0'), the conversion is terminated and the result is returned as the return value.
  • Parameters:
    nptr:String to be converted
  • Return value: Integer after successful conversion

Similar functions are:

  • atof():Converts a decimal string to a floating point number.
  • atol():Convert a string tolongtype

Example 1:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
    
    
	// atoi函数
	// 0-9的字符和-+号就开始转,如果不是+-0-9的字符就结束,如果前面有空格,就跳过
	char a[] = "1234";
	char b[] = "-1234";
	char c[] = "e1234";
	char d[] = " 1234";
	printf("a=%d\n", atoi(a));
	printf("b=%d\n", atoi(b));
	printf("c=%d\n", atoi(c));
	printf("d=%d\n", atoi(d));
	// atof函数
	char e[] = "3.14";
	printf("e=%f\n", atof(e));
	// atol函数
	char f[] = "11321321";
	printf("f=%ld\n", atol(f));
	return 0;
}
输出结果
a=1234
b=-1234
c=0
d=1234
e=3.140000
f=11321321

8) Common string application models for project development

8.1 while and do-while models in strstr

Usestrstr standard library function to find the number of timesabcd appears in a string.

while model

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


int main() {
    
    

	char* p = "32e23abcd11132122abcd333abcd9922abcd333abc32d2qqq";
	int n = 0;
	// 查找匹配到到则进去,循环条件第一次从字符串首元素地址开始查找
	while ((p=strstr(p, "abcd"))!=NULL)
	{
    
    	
		// 进入循环后,说明此时已经匹配到一个了,那么就要重新设置查找起点位置
		p += strlen("abcd");
		n++;
	}
	printf("n=%d", n);
	return 0;
}
输出结果
n=4

do-while model

int main() {
    
    

	char* p = "32e23abcd11132122abcd333abcd9922abcd333abc32d2qqq";
	int n = 0;
	do
	{
    
    
		p = strstr(p, "abcd");
		if (p!=NULL)
		{
    
    	
			p += strlen("abcd");
			n++;
		}
		
	} while (p!=NULL);
	printf("n=%d", n);
	return 0;
}
输出结果
n=4

8.2 Two-end blocking model

Find the number of non-empty string elements:

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

int main() {
    
    

	char str[] = "    hello cdtaogang     ";//hello cdtaogang
    char buf[128] = "";
	// 判断空字符
    if (str[0] == 0)
		return;
    char * begin = str; // 数组首元素地址,也就是起始位置
	// str字符串的长度-1就表示最后一个元素的下标,取最后一个字符的地址
    char *end = &str[strlen(str) - 1];//end指向最后一个字符
    //从起始位置开始找,找到第一个不是空格的位置,并且不能移出数组最后一个位置\0
    while (*begin == ' ' &&  *begin != 0)
    {
    
    
		begin++;
    }
	//从结束位置开始找,找到第一个不是空格的位置,并且不能等于begin字符串的起始位置
    while (*end == ' '  &&  end != begin)
    {
    
    
            end--;
    }
	// 打印数组的长度,字符结束位置-起始位置+1得到字符串的长度
    printf("%d\n",end-begin +1);
	// 将匹配出来的字符串拷贝到buf数组中
    strncpy(buf, begin, end-begin +1);
    printf("buf=%s\n", buf);
    system("pause");

	return 0;
}
输出结果
15
buf=hello cdtaogang

8.3 String reversal model (inversion)

Insert image description here

#include <stdio.h>


int main() {
    
    

	char str[] = "abcdef";

	char* begin = str;
	char* end = &str[strlen(str) - 1];
	printf("%c %c\n", *begin, *end);  // a f
	printf("str=%s\n", str);
	while (end > begin)
	{
    
    	
		//交换元素
		char ch = *begin;
		*begin = *end;
		*end = ch;
		begin++;
		end--;
	}
	printf("str=%s\n", str);

	return 0;
}

Insert image description here

Summary of pointers

definition illustrate
int i Define integer variable
int *p Define a pointer variable pointing to int
int a[10] Define an array with 10 elements, each element is of type int
int *p[10] Define an array with 10 elements, each element is of type int*
int func() Define a function with a return value of type int
int *func() Define a function with a return value of type int *
int **p Define a pointer to a pointer to int, a second-level pointer (that is, the address pointed to by p stores a first-level pointer to int)

Guess you like

Origin blog.csdn.net/qq_41782425/article/details/127894938