当前位置:网站首页>C language string and pointer - learning 25
C language string and pointer - learning 25
2022-06-12 00:39:00 【XG. Solitary dream】
This paper is finally updated at 2022 year 02 month 20 Japan , Has more than 7 I didn't update it . If the article content or picture resources fail , Please leave a message for feedback , I will deal with it in time , thank you !
How strings are referenced
- Strings are stored in character arrays .
- Reference a string , There are two ways to do this :
- use A character array Store a string , Can pass Array name and format declaration “%s” The output string can also refer to a character in the string through the array name and subscript .
- use Character pointer variable Point to A string constant , adopt Character pointer variables refer to string constants .
- Example
#include <stdio.h>
void main() {
char string[] = "I love China!";
printf("%s\n", string);
printf("%c\n", string[7]); // Output eighth character
}
// Output a string through a character pointer
#include <stdio.h>
void main() {
char *string = "I love China!";
printf("%s\n", string);
printf("%c\n", string[7]); // Output eighth character
}take a The string array is copied to b Array of strings
#include <stdio.h>
void main() {
char a[] = "abcdefg";
char b[20];
int i;
for (i = 0; *(a + i) != '\0'; i++) {
*(b + i) = *(a + i);
}
*(b + i) = '\0';
printf("a = %s\n", a);
// printf("b = %s\n", b);
printf("b = ");
for (i = 0; b[i] != '\0'; i++) {
printf("%c", b[i]);
}
printf("\n");
}Handle with pointer variables
#include <stdio.h>
void main() {
char a[] = "abcdefg", b[20],*p1,*p2;
p1 = a;
p2 = b;
for (; *p1 != '\0'; p1++) {
*p2 = *p1;
p2++;
}
*p2 = '\0';
printf("a = %s\n", a);
printf("b = %s\n", b);
}Comparison of character pointer variable and character array
- use A character array and Character pointer variable Can achieve String storage and operation ; There is a difference between them , There are mainly the following points :
- 1. A character array from Several elements form , Put one character in each element ; and Character pointer variable Where is the Address ( String number 1 A character address ), Instead of putting strings in character pointer variables .
- 2. Different assignment methods , You can assign values to character pointer variables , But you cannot assign values to array names .
char *a;a="I love Chinal";correctchar str[14];str[0]='1';correctchar str[14]; str = "I love China!";error
- 3. The contents of the storage unit are different , Compiled as A character array Distribute Several storage units To store the values of each element , And yes Character pointer variable , Just assign One storage unit .
char *a, str[10];a=str;scanf ("%s",a);correctchar *a; scanf("%s",a);error
- 4. Pointer to the variable The value of is You can change Of , and Array name Represents a fixed value ( That is, the address of the first element of the array ) Can't change .
- Example
#include <stdio.h
void main() {
char *a = "I love China!";
a = a + 7;
printf("%s\n", a); // Output from the eighth character
}- 5. A character array in The value of each element can be changed Of , but Character pointer variable Point to the The contents of string constants cannot be replaced Of .
char a[]="House", *b="House";a[2]='r';correctb[2]='r';error
- 6. Reference array elements
- For character arrays, you can use The subscript method and Address method Reference array elements .
- for example :
a[5]、*(a+5)
- for example :
- If the character pointer variable
p=a, You can also use Pointer variable with subscript Form and Address method quote .- for example :
p[5]、*(p+5)
- for example :
- For character arrays, you can use The subscript method and Address method Reference array elements .
- 7. use The pointer variable points to a format string , You can use it Instead of printf Function Format string .
- Example
#include <stdio.h>
void main() {
char *format;
int a = 10;
float b = 3.6;
format = "a = %d, b = %f \n";
printf(format, a, b);
// amount to
printf("a = %d, b = %f \n", a, b);
}Character pointer as function parameter
- If you want to put the A string from A function “ Pass on ” To Another function , It can be used Address delivery The way to , The box Character array name do Parameters , It can also be used. Character pointer variable do Parameters .
- stay Called In the function of Change the contents of the string , stay The main function Medium can Reference the changed string .
- Example
Using function call to copy string .
#include <stdio.h>
void main(){
// Character array names are used as parameters
void string_copy(char from[], char to[]);
char a[] = "abcdefg", b[20];
string_copy(a, b);
printf("a = %s\n", a);
printf("b = %s\n", b);
}
void string_copy(char from[], char to[]){
int i;
for (i=0; from[i]!= '\0'; i++) {
to[i] = from[i];
}
to[i] = '\0';
}#include <stdio.h>
// Character pointer variables are used as parameters
void main(){
void string_copy(char *from, char *to);
char *a = "abcdefg", b[20];
char *to = b;
string_copy(a, to);
printf("a = %s\n", a);
printf("b = %s\n", b);
}
void string_copy(char *from, char *to){
for (; *from != '\0'; from++,to++) {
*to = *from;
}
*to = '\0';
}Pointer to function
- stay C In language , A function always occupies a contiguous area of memory , and Function name Is the memory area occupied by this function The first address .
- We can give this The first address ( Or entrance address ) Give a pointer variable , Make the pointer variable point to the function .
- And then through Pointer to the variable Can Find and call this function . We call the pointer variable of this number “ Function pointer variable ”.
Call function with function pointer variable
- Definition of function pointer variable
- The general form is :
Type specifier(* Pointer variable name )()
- Type specifier : Represents the type of the return value of the referred function
- * Pointer variable name :“*” The following variables are defined pointer variables
- (): A pointer variable refers to a function
- The general form is :
- for example :
int (*pf)();Express pf Is a pointer variable to the function entry ,( Function value ) Integer. . - Example
#include <stdio.h>
// Character pointer variables are used as parameters
void main(){
int max(int, int);
int(*p)(int, int);
int a, b, c;
p = max;
scanf_s("%d %d", &a, &b);
c = (*p)(a, b);
printf("max=%d\n", c);
}
int max(int x,int y){
int z;
z = (x > y) ? x : y;
return (z);
}边栏推荐
- [answer] is ubiquitous language a pseudo innovation?
- APP项目怎么测?如何搭建测试体系
- Jeecgboot 3.1.0 release, enterprise low code platform based on code generator
- 验证码是自动化的天敌?看看阿里P7大神是怎么解决的
- ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
- Flutter 使用本地图片
- The long polling processing mechanism of the service end of the # yyds dry goods inventory # Nacos configuration center
- 设计消息队列存储消息数据的 MySQL 表格
- Motianlun domestic database salon | Xu Li: Alibaba cloud's native lindorm TSDB database drives the upgrading of industrial it & ot hyper integrated digital system
- Wechat applet Chinese English conversion
猜你喜欢

IP addressing overview
![[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC](/img/e0/0326d01fc78ed2f96a475e28e74d1c.jpg)
[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC

How to cancel smart table style in WPS table

Motianlun domestic database salon | Xu Li: Alibaba cloud's native lindorm TSDB database drives the upgrading of industrial it & ot hyper integrated digital system

模块八-设计消息队列存储消息数据的 MySQL 表格

How to uninstall pscs6 in win10 system

Exploration of qunar risk control safety products

汛期化工和危险品企业如何加强防控重大安全风险

基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计

leetcodeSQL:614. Secondary followers
随机推荐
Jeecgboot 3.1.0 release, enterprise low code platform based on code generator
Point cloud library PCL from introduction to mastery learning records Chapter 8
[foreign enterprise test interview and written examination] share the whole process of 8 rounds of 30k+ foreign enterprise interview
Water for a while
关于接口测试的那些“难言之隐”
设计消息队列存储消息数据的 MySQL 表格
多年测试菜鸟对黑盒测试的理解
Investment analysis and prospect Trend Research Report of global and Chinese cyclopentanyl chloride industry 2022-2028
出门带着小溪
苹果手机wps文件如何发送到qq邮箱
IP addressing overview
Zhangxiaobai takes you to install MySQL 5.7 on Huawei cloud ECS server
组态王如何利用无线Host-Link通信模块远程采集PLC数据?
Industrial control system ICs
一、Flutter 入门学习写一简单客户端
2022 edition of global and Chinese high purity silicon carbide powder operation research and investment strategy analysis report
How to optimize plantuml flow diagram (sequence diagram)
wps表格怎么取消智能表格样式
Breadth first search depth first search dynamic programming leetcode topic: delivering information
Share an open source, free and powerful video player library