当前位置:网站首页>指针进阶,字符串函数
指针进阶,字符串函数
2022-07-07 06:00:00 【epsilon279】
指针笔试题
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;
}
字符串函数
strlen
size_t strlen ( const char * str );
字符串已经 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包
含 ‘\0’ )。
参数指向的字符串必须要以 ‘\0’ 结束。
注意函数的返回值为size_t,是unsigned int.
strlen使用中的易错点
strlen 模拟实现
//1.计数器
#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.指针-指针
#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.递归
#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).
源字符串必须以 ‘\0’ 结束。
会将源字符串中的 ‘\0’ 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
目标空间必须可变。
学会模拟实现。
int main()
{
char name[20] = {
0 };
name = "zhangsan";//err,name数组名是地址,地址是常量值,不能修改,不能被赋值
printf("%s\n", name);
return 0;
}
目标空间必须可变。
int main()
{
const char* p = "abcdef";//err,常量字符串不能修改
char arr[] = "bit";
strcpy(p, arr);
return 0;
}
指向字符串的指针不能用来修改此字符串,会出错。
这是因为:
char *p=“hello”; 等价于 const char *m=“hello”;
对于指针p,无非就是一个地址的拷贝,也就是"hello"地址的拷贝。
"hello"保存在静态的存储区,该数据不能修改。
故不能通过指针p修改数据区的值
为何char a[ ]可以修改字符串
这是因为: “hello” 保存在栈空间数组里,数组名为a, 数组名为数组的首地址。
char a[]=“hello”; 是从静态存储区(常量区)复制内容副本(即hello)到栈里给了 a[] 所以
所以a[] 本身有一个自己的 hello 副本,可以对其进行想要的合法操作,例如:改变字符串的内容。
strcpy 模拟实现
strcat
char * strcat ( char * destination, const char * source );
源字符串必须以 ‘\0’ 结束。
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。
strcat 模拟实现
字符串自己给自己追加,如何?
不可行,因为拷贝字符串过程中’\0’被覆盖,自身的内容被破坏,缺少’\0’,陷入死循环。
边栏推荐
- National SMS center number inquiry
- 【微信小程序:缓存操作】
- 数据中台落地实施之法
- All about PDF crack, a complete solution to meet all your PDF needs
- Snyk dependency security vulnerability scanning tool
- Data type - floating point (C language)
- [Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
- SSM integration
- Data type - integer (C language)
- Open3d ISS key points
猜你喜欢
2-3查找树
Golang 编译约束/条件编译 ( // +build <tags> )
登山小分队(dfs)
单场带货涨粉10万,农村主播竟将男装卖爆单?
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Iptables' state module (FTP service exercise)
Train your dataset with swinunet
All about PDF crack, a complete solution to meet all your PDF needs
Arm GIC (IV) GIC V3 register class analysis notes.
随机推荐
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
数据中台落地实施之法
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Redis summary
Tuowei information uses the cloud native landing practice of rainbow
GFS分布式文件系统
Open3D ISS关键点
Thirteen forms of lambda in kotlin
归并排序和非比较排序
All about PDF crack, a complete solution to meet all your PDF needs
2 - 3 arbre de recherche
opencv 将16位图像数据转为8位、8转16
[南京大学]-[软件分析]课程学习笔记(一)-introduction
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
let const