C语言-str家族之strlen,strcpy,strncpy


/* PC上食用更佳哦!

       代码目录:linux4.9.88内核lib目录 

       date:2022年7月6日19:50:25

*/

一、strlen

太简单都懒得介绍了,还是介绍一下吧

功能:用来返回字符串的长度

直接上内核源码:

/**
 * strlen - Find the length of a string
 * @s: The string to be sized
 */
size_t strlen(const char *s)
{
	const char *sc;

	for (sc = s; *sc != '\0'; ++sc)
		/* nothing */;
	return sc - s;
}

关于 *sc != '\0',大家应该都知道字符串的结尾是结束符\0而不是最后一个字符,所以遇到结束符时表示到最后了

使用示例及结果:

#include <stdio.h>
#include <string.h>

void main(){
    char *a = "Ac";
    char b  = 'A';
    
	printf("strlen(a) =  %ld\n", strlen(a));
	printf("sizeof(a) =  %ld\n", sizeof(a));
	//printf("strlen(b) =  %d\n", strlen(b));这个语句不能使用,因为strlen()的参数是const char *s,b只是个char型变量
	printf("sizeof(b) =  %ld\n", sizeof(b));
}

结果:
strlen(a) =  2
sizeof(a) =  8
sizeof(b) =  1

顺便说一下sizeof(),sizeof是用来求变量大小的,上面代码中
变量b是char类型,char是1字节,结果就是1

对sizeof(a) = 8,应该是64位的,所以是8。

而a的长度,就是字符串的长度,两个字符就是2啦
当然也可以sizeof(int),sizeof(char)等等。

#include <stdio.h>
#include <string.h>

int main(){
    int a[3] = {1,2,3};
    char b[]  = "Ac";
    
	printf("strlen(a) =  %ld\n", sizeof(a)/sizeof(typeof(a[0])));
	printf("sizeof(a) =  %ld\n", sizeof(a));
	printf("strlen(b) =  %ld\n", strlen(b));
	printf("sizeof(b) =  %ld\n", sizeof(b));
	return 0;
	
结果:
strlen(a) =  3	//a数组的元素个数
sizeof(a) =  12	//数组的大小(并不是元素个数,元素是3个,如果是a[6] = {1, 2, 3},就是24字节)3个4字节的int数据,共12字节
strlen(b) =  2	//b的字符个数,即长度,2
sizeof(b) =  3	//b的大小,前面说过有个结束符,所以是3

}

二、strcpy

源码如下:

/**
 * strcpy - Copy a %NUL terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 */
#undef strcpy
char *strcpy(char *dest, const char *src)
{
	char *tmp = dest;

	while ((*dest++ = *src++) != '\0')
		/* nothing */;
	return tmp;
}

作用:将src中的内容复制到dest中

使用示例及结果:

#include <stdio.h>
#include <string.h>
 
int main()
{
   char src[40] = {0};
   char dest[100] = {0};
  
   strcpy(src, "ABCDEFG");
   strcpy(dest, src);
 
   printf("dest: %s\n", dest);
   
   return(0);
}

在这里插入图片描述
使用这个函数要注意,src<=dest,这是防止内存践踏,如果src大于dest,dest空间就放不下,多出的内容就要紧挨着dest放,如果这块空间有其他变量在使用,那么它就意外的被改变了。
看看该函数的描述

DESCRIPTION
       The strcpy() function copies the string pointed to by src, including the terminating
       null byte ('\0'), to the buffer pointed to by dest.  The strings  may  not  overlap,
       and the destination string dest must be large enough to receive the copy.  Beware of
       buffer overruns!  (See BUGS.)

三、strncpy

strncpy和strcpy是兄弟俩,源码中strncpy定义如下

/**
 * strncpy - Copy a length-limited, C-string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @count: The maximum number of bytes to copy
 *
 * The result is not %NUL-terminated if the source exceeds
 * @count bytes.
 *
 * In the case where the length of @src is less than  that  of
 * count, the remainder of @dest will be padded with %NUL.
 *
 */
char *strncpy(char *dest, const char *src, size_t count)
{
	char *tmp = dest;

	while (count) {
		if ((*tmp = *src) != 0)
			src++;
		tmp++;
		count--;
	}
	return dest;
}

man手册中的说明


The strncpy() function is similar, except that at most n bytes of  src  are  copied.
       Warning:  If there is no null byte among the first n bytes of src, the string placed
       in dest will not be null-terminated.

       If the length of src is less than n, strncpy() writes additional null bytes to  dest
       to ensure that a total of n bytes are written.

1、strncpy拷贝src的前count个字节到dest中
2、当count大于src时,为了凑够count个数,会补上空值,即NULL。
3、当count小于src时,会拷贝count个值到dest中,但是dest没有结束符,srncpy不会自动给dest添加结束符
注意:当数组未初始化时,如char dest[];使用strncpy前要做这一步dest[count] = '\0';否则dest会乱码,如果数组初始化了char dest[10] = {0};则无需做这一步
使用示例及结果:数组初始化

#include <stdio.h>
#include <string.h>
 
int main()
{
	char src[] = {"ABCDE"};
	char dest[10] = {0};

	strncpy(dest,src,8);
	
	printf("dest: %s\n", dest);
	printf("strlen(dest): %d\n", strlen(dest));

	return 0;
}

这里拷贝count为8,src长度小于count

dest: ABCDE
strlen(dest)5

当修改为strncpy(dest,src,3);

dest: ABC
strlen(dest)3

使用示例及结果:数组未初始化

#include <stdio.h>
#include <string.h>
 
int main()
{
	char src[] = {"ABCDE"};
	char dest[10];
	//dest[3] = '\0';
	strncpy(dest,src,3);
	
	printf("dest: %s\n", dest);
	printf("strlen(dest): %d\n", strlen(dest));
	
	return 0;
}
dest: ABC烫烫烫烫藺BCDE
strlen(dest)17

总结:
1、strcpy使用时切勿使src>dest,防止内存越界,strcpy不需要设置’\0’哦
2、strncpy使用时,除了防止越界还有以下小技巧
a)数组初始化时,尽量使用strncpy(dest,src,sizeof(dest)-1);最多也就拷贝dest-1长度,问为什么要减1,因为字符串要以结束符’\0’结束啊,实际上char dest[10]也就只有9个有效长度的字符
b)数组未初始化时,提前把结束符放好dest[count] = '\0';

本人新手一只,如有错误欢迎指出!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值