当前位置:网站首页>Pointer advanced, string function
Pointer advanced, string function
2022-07-07 08:47:00 【epsilon279】
List of articles
Pointer written test questions
1.

2.

3.
int main()
{
int a[4] = {
1, 2, 3, 4 };
int *ptr1 = (int *)(&a + 1);
int *ptr2 = (int *)((int)a + 1);
printf( "%x,%x", ptr1[-1], *ptr2);
return 0;
}


4.
#include <stdio.h>
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };
int *p;
p = a[0];
printf( "%d", p[0]); //1
return 0;
}

5.
int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf( "%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
return 0;
}


6.
int main()
{
int aa[2][5] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int *ptr1 = (int *)(&aa + 1);
int *ptr2 = (int *)(*(aa + 1));
printf( "%d,%d", *(ptr1 - 1), *(ptr2 - 1));
return 0;
}

7.
#include <stdio.h>
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}

8.
int main()
{
char* c[] = {
"ENTER","NEW","POINT","FIRST" };
char** cp[] = {
c + 3,c + 2,c + 1,c };
char*** cpp = cp;
printf("%s\n", **++cpp);
printf("%s\n", *-- * ++cpp + 3);
printf("%s\n", *cpp[-2] + 3);
printf("%s\n", cpp[-1][-1] + 1);
return 0;
}

String function
strlen
size_t strlen ( const char * str );
The string has ‘\0’ As an end sign ,strlen Function returns in a string ‘\0’ The number of characters that appear before ( No package
contain ‘\0’ ).
The string that the argument points to must be in ‘\0’ end .
Note that the return value of the function is size_t, yes unsigned int.


strlen Error prone points in use

strlen Simulation Implementation
//1. Counter
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str);
size_t count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
size_t n=my_strlen(arr);
printf("%u\n", n);//6
return 0;
}
//2. The pointer - The pointer
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str);
char* start = str;
size_t count = 0;
while (*str != '\0')
{
count++;
str++;
}
char* end = str;
return end-start;
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
//3. recursive
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str);
if (*str != '\0')
{
str++;
return (1 + my_strlen(str));
}
else
return 0;
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
strcpy
char * strcpy ( char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the
terminating null character (and stopping at that point).
The source string must be in ‘\0’ end .
In the source string ‘\0’ Copy to target space .
The target space has to be large enough , To ensure that the source string can be stored .
The target space has to be variable .
Learn to simulate .
int main()
{
char name[20] = {
0 };
name = "zhangsan";//err,name The array name is the address , The address is a constant value , Do not modify , Cannot be assigned
printf("%s\n", name);
return 0;
}
The target space has to be variable .
int main()
{
const char* p = "abcdef";//err, Constant string cannot be modified
char arr[] = "bit";
strcpy(p, arr);
return 0;
}
A pointer to a string cannot be used to modify this string , Will make mistakes .
This is because :
char *p=“hello”; Equivalent to const char *m=“hello”;
For the pointer p, It's nothing more than a copy of an address , That is to say "hello" Copy of address .
"hello" Stored in static storage , This data cannot be modified .
Therefore, it cannot pass through the pointer p Modify the value of the data area
why char a[ ] You can modify the string
This is because : “hello” Stored in the stack space array , Array name a, The array name is the first address of the array .
char a[]=“hello”; From static storage ( The constant area ) Copy content ( namely hello) To the stack a[] therefore
therefore a[] It has its own hello copy , You can perform the desired legal operation , for example : Change the contents of the string .
strcpy Simulation Implementation

strcat
char * strcat ( char * destination, const char * source );
The source string must be in ‘\0’ end .
The target space must be large enough , It can hold the contents of the source string .
The target space must be modifiable .
strcat Simulation Implementation

The string appends itself , how ?
Is not workable , Because in the process of copying strings ’\0’ Be overwritten , Its own content is destroyed , The lack of ’\0’, Fall into a dead cycle .
边栏推荐
- Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
- Opencv converts 16 bit image data to 8 bits and 8 to 16
- 2-3查找樹
- You should use Google related products with caution
- Tips for using jeditabletable
- [Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
- 联想混合云Lenovo xCloud:4大产品线+IT服务门户
- In go language, function is a type
- go写一个在一定时间内运行的程序
- [Yugong series] February 2022 U3D full stack class 006 unity toolbar
猜你喜欢

IP地址的类别

A bug using module project in idea
![[step on the pit] Nacos registration has been connected to localhost:8848, no available server](/img/ee/ab4d62745929acec2f5ba57155b3fa.png)
[step on the pit] Nacos registration has been connected to localhost:8848, no available server

南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案

快速集成认证服务-HarmonyOS平台

Train your dataset with swinunet

opencv之图像分割

为什么要选择云原生数据库
![Other 7 features of TCP [sliding window mechanism ▲]](/img/ff/c3f52a7b89804acfd0c4f3b78bc4a0.jpg)
Other 7 features of TCP [sliding window mechanism ▲]

Why choose cloud native database
随机推荐
详解华为应用市场2022年逐步减少32位包体上架应用和策略
关于基于kangle和EP面板使用CDN
Train your dataset with swinunet
Qt Charts使用(重写QChartView,实现一些自定义功能)
路由信息协议——RIP
JS operation
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Gson转换实体类为json时报declares multiple JSON fields named
Basic data types and string types are converted to each other
ncs成都新电面试经验
Greenplum6.x-版本变化记录-常用手册
Implementation method of data platform landing
Quick sorting (detailed illustration of single way, double way, three way)
Greenplum6.x重新初始化
国标GB28181协议视频平台EasyGBS新增拉流超时配置
Count sort (diagram)
Gson converts the entity class to JSON times declare multiple JSON fields named
Routing information protocol rip
Data type - floating point (C language)
Interpolation lookup (two methods)