strncpy和strlen的可能的实现
#include <stdio.h>
#include <stdlib.h>
//为避免与标准库中的函数发生混淆,我将它们命名为stringNCopy和stringLength
char *stringNCopy(char *dest, const char *src, size_t count)
{
for (size_t i = 0; i < count; ++i)
{
dest[i] = src[i];
if (!src[i])
{
break;
}
}
return dest;
}
size_t stringLength(const char *str)
{
size_t count = 0;
while (*str++)
{
++count;
}
return count;
}
int main()
{
char *dest = malloc(sizeof(char) * 100);
const char *src = "hello world\n世界你好";
stringNCopy(dest, src, stringLength(src) + 1);
printf("%s\n", dest);
free(dest);
char arr[50];
char *ret = stringNCopy(arr, "programming 程序设计", 10000);
printf("%s\n%s\n", arr, ret);
ret = stringNCopy(arr, "Programmer", 1);
printf("%s\n%s\n", arr, ret);
char test[2];
const char *str = "测试\0Test";
ret = stringNCopy(test, str, 100); //非法的操作,可通过编译,但是运行之后可能会报错(vc++下编译并运行之后会报错,Windows下gcc编译后运行无报错)
printf("%s\n%s\n", test, ret);
printf("%u\n%u\n%u\n", stringLength(""), stringLength("\0\0\0"), stringLength("字符串 String"));
return 0;
}
strncpy和strlen的可能的实现的更多相关文章
- strcpy、strncpy、strlen、memcpy、memset、strcat、strncat、strcmp、strncmp,strchr
1.strcpy #include<stdio.h> #include<assert.h> char *mystrcpy(char *dest, const char *src ...
- Linux进程间通信—共享内存
五.共享内存(shared memory) 共享内存映射为一段可以被其他进程访问的内存.该共享内存由一个进程所创建,然后其他进程可以挂载到该共享内存中.共享内存是最快的IPC机制,但由于linux本身 ...
- http协议中content-length 以及chunked编码分析
转载请注明出处 http://blog.csdn.net/yankai0219/article/details/8269922 0.序 1.http/1.1协议中与chunked编码的相关字段 1)E ...
- 菜鸟 学注册机编写之 “MD5”
测试环境 系统: xp sp3 调试器 :od 1.10 sc_office_2003_pro 高手不要见笑,仅供小菜玩乐,有不对或不足的地方还请多多指教,不胜感激! 一:定位关键CALL 1. 因 ...
- 《嵌入式linux应用程序开发标准教程》笔记——8.进程间通信
, 8.1 概述 linux里使用较多的进程间通信方式: 管道,pipe和fifo,管道pipe没有实体文件,只能用于具有亲缘关系的进程间通信:有名管道 named pipe,也叫fifo,还允许无亲 ...
- gperftools cpp wrapper
gperftools cpp wrapper // Compile command : ${CXX} -o test_profiler.elf -DUSE_GPERFTOOLS -DDEBUG -D_ ...
- CVE-2013-2094 porting to x86-32 分析
/* * linux 2.6.37-3.8.8 - x86 * @rikiji * * requires System.map and /dev/ptmx * this: http://zmbs.ne ...
- 转:Linux--进程间通信(信号量,共享内存)
源地址:http://www.cnblogs.com/forstudy/archive/2012/03/26/2413724.html Linux--进程间通信(信号量,共享内存)(转) 一. 信 ...
- c js 字符串反转
1.例如:输入 i am a student 输出 student a am i #include <stdio.h> #include <string.h> v ...
随机推荐
- Python之Regular Expressions(正则表达式)
在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 很可能你使用过Windows/Dos下用 ...
- HDU——2955Robberies(小数背包)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- BZOJ 3527 [Zjoi2014]力 ——FFT
[题目分析] FFT,构造数列进行卷积,挺裸的一道题目诶. 还是写起来并不顺手,再练. [代码] #include <cmath> #include <cstdio> #inc ...
- Generation I
Generation I Oak is given N empty and non-repeatable sets which are numbered from 1 to N. Now Oak is ...
- LVM 类型的 Storage Pool
LVM 类型的 Storage Pool 不仅一个文件可以分配给客户机作为虚拟磁盘,宿主机上 VG 中的 LV 也可以作为虚拟磁盘分配给虚拟机使用. 不过,LV 由于没有磁盘的 MBR 引导记录,不能 ...
- C#.net磁盘管理以及文件操作
原文发布时间为:2008-08-08 -- 来源于本人的百度文章 [由搬家工具导入] 需要引入的命名空间: using System.IO;using System.Text; private ...
- 标准C程序设计七---31
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- PSO(Thepopularity-similarity-oplimization) modol
PSO(Thepopularity-similarity-oplimization) modol 在这篇文章里,我们试图将社交关系构成的网络结构从纷繁复杂的具体场景.细节条件中剥离出来,单单从个体间连 ...
- cssnext下一代的css
前端技术更新迭代的速度令人咂舌,互联网+的风头刚起那几年,前端技术大多还停留在jquery阶段,按需加载还停留在seajs和requirejs的阶段,css3和H5也不过才崭露头角,但经过几年的飞速发 ...
- DDCTF2019逆向分析前俩题WriteUP
DDCTF2019 笔者做了前俩道题.冷不丁过去一个月了.现在在此做一下WriteUp:题目链接: 1:题目1 2:题目2 reverse1:writeup: 1.程序打开后如下所示 2.查壳结果为U ...