鱼C论坛

 找回密码
 立即注册
查看: 2269|回复: 1

strlen函数问题

[复制链接]
发表于 2018-1-14 21:19:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是源代码,问题在下面,请描述详细点,谢谢

/* test.c 试用缩短字符串的函数*/
#include<stdio.h>
#include<string.h>/*该头文件中包含字符串函数的原型*/
void fit(char*,unsigned int);
int main(void)
{
char mesg[]={
"Hold on to your hats,hackers."
};
puts(mesg);
fit(mesg,7);
puts(mesg);
puts("let's look at some more of the string.");
puts(mesg+8);
return 0;
}
void fit(char*string,unsigned int size)
{
if(strlen(string)>size)
{
*(string+size)='\0';
}
}

输出如下:

Hold on to your hats,hackers.
Hold on
let's look at some more of the string.

to your hats,hackers.



下面这个(*(string+size)='\0';)表示什莫意思?第二个puts(mesg);为什莫输出Hold on ? 谢谢!
if(strlen(string)>size)
{
*(string+size)='\0';
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-14 21:29:04 From FishC Mobile | 显示全部楼层
字符串的结尾都是'\0'
这个fit函数的作用就是根据给的长度size将给的字符串string的第size个元素后面设置为'\0'
当再次读取这个字符串时就只能读取前面的字符了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-1 07:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表