当前位置:网站首页>指针进阶,字符串函数
指针进阶,字符串函数
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’,陷入死循环。
边栏推荐
- 使用AGC重签名服务前后渠道号信息异常分析
- 数据分析方法论与前人经验总结2【笔记干货】
- [Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
- Opencv learning note 4 - expansion / corrosion / open operation / close operation
- Rsync remote synchronization
- 2-3 lookup tree
- Open3d ISS key points
- [untitled]
- GFS distributed file system
- uniapp 微信小程序监测网络
猜你喜欢

SSM integration

Using nocalhost to develop microservice application on rainbow

Opencv learning note 5 - gradient calculation / edge detection

Implementation of navigation bar at the bottom of applet

Arm GIC (IV) GIC V3 register class analysis notes.

关于基于kangle和EP面板使用CDN

IP地址的类别

SSM 整合

Data type - floating point (C language)

Golang 编译约束/条件编译 ( // +build <tags> )
随机推荐
Thirteen forms of lambda in kotlin
Golan idea IntelliJ cannot input Chinese characters
Arm GIC (IV) GIC V3 register class analysis notes.
Compilation and linking of programs
IP地址的类别
如何在快应用中实现滑动操作组件
[kuangbin] topic 15 digit DP
单场带货涨粉10万,农村主播竟将男装卖爆单?
注解@ConfigurationProperties的三种使用场景
MES系统,是企业生产的必要选择
Gson转换实体类为json时报declares multiple JSON fields named
联想混合云Lenovo xCloud:4大产品线+IT服务门户
String operation
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
Using nocalhost to develop microservice application on rainbow
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
SSM integration
Three series of BOM elements
基本数据类型和string类型互相转化