当前位置:网站首页>指针进阶,字符串函数
指针进阶,字符串函数
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’,陷入死循环。
边栏推荐
- POJ - 3616 Milking Time(DP+LIS)
- [南京大学]-[软件分析]课程学习笔记(一)-introduction
- Tips for using jeditabletable
- Rapid integration of authentication services - harmonyos platform
- [machine learning] watermelon book data set_ data sharing
- 【MySQL】数据库进阶之触发器内容详解
- Other 7 features of TCP [sliding window mechanism ▲]
- Arm GIC (IV) GIC V3 register class analysis notes.
- Give full play to the wide practicality of maker education space
- Redis summary
猜你喜欢

2-3查找树

联想混合云Lenovo xCloud:4大产品线+IT服务门户

Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot

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"

Novice entry SCM must understand those things

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

Input of mathematical formula of obsidan

打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”

SSM integration
![[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?](/img/ff/81a7b2ec08a6a422a5cf578c1fed13.jpg)
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
随机推荐
Splunk query CSV lookup table data dynamic query
Ebpf cilium practice (2) - underlying network observability
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
为什么要选择云原生数据库
One click installation of highly available Nacos clusters in rainbow
mysql分区讲解及操作语句
详解华为应用市场2022年逐步减少32位包体上架应用和策略
如何在快应用中实现滑动操作组件
使用AGC重签名服务前后渠道号信息异常分析
Learn how to compile basic components of rainbow from the source code
Opencv learning notes II - basic image operations
[untitled]
Teach you how to select PCB board by hand (II)
Opencv learning notes 1 -- several methods of reading images
Opencv learning note 5 - gradient calculation / edge detection
登山小分队(dfs)
关于基于kangle和EP面板使用CDN
iptables 之 state模块(ftp服务练习)
Basic data types and string types are converted to each other
Leetcode 1984. Minimum difference in student scores