zoukankan
html css js c++ java
C++ 编写strcpy函数
小小的一段strcpy函数,却能看出不少的问题。
在此作一下备忘:
#include
<
iostream
>
#include
<
string
>
using
namespace
std;
char
*
str_copy(
char
*
dest,
const
char
*
src);
int
main()
{
char
*
src
=
"
this is a test
"
;
cout
<<
strlen(src)
<<
endl;
//
申请内存大小时,需要在源串的长度上加1,以存放\0
char
*
test
=
(
char
*
)(malloc(strlen(src)
+
1
));
cout
<<
sizeof
(test)
<<
endl;
str_copy(test,src);
if
(test
!=
NULL)
{
cout
<<
test
<<
endl;
}
else
{
cout
<<
"
No Enters
"
<<
endl;
}
free(test);
//
for console test
char
c;
cin
>>
c;
//
end test
return
0
;
}
;
char
*
str_copy(
char
*
dest,
const
char
*
src)
{
if
(dest
==
NULL)
{
return
NULL;}
if
(src
==
NULL
||
*
src
==
'
\0
'
)
{
return
NULL;}
char
*
testTemp
=
dest;
while
((
*
testTemp
++=*
src
++
)
!=
'
\0
'
);
return
testTemp;
}
<h3>
心静似高山流水不动,心清若巫峰雾气不沾。
</h3>
查看全文
相关阅读:
leetcode Majority Element
Missing Number 三种解法
Effective C++学习笔记 chapter 1
C++ 笔记
三色排序
归并排序-就地排序
506,display有哪些值?说明他们的作用
505,display,float,position之间的关系(有疑问)
504,什么是FOUC?怎么避免
503,display:none;与visibility:hidden;的区别
原文地址:https://www.cnblogs.com/McJeremy/p/1432190.html
最新文章
系统的结构化分析与设计方法
系统需求建模:事件和事物
software Architecture(3)
什么样的男人才算是成熟的男人?
如何在人多的稍陌生聚会场合表现得自信、自如、外向?
组织行为学4
组织行为学3
software Architecture(2)
Linux基本命令运行
关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist
热门文章
解决maven项目Invalid bound statement (not found)的方法
IDEA配置好maven后新建maven项目一直build失败的解决方法
Unix中的特殊文件及文件属性
Unix中的特殊文件及文件属性
unix 基础知识
unix 基础知识
leetcode-Container With Most Water
leetcode
Effective C++学习笔记 chapter 3
Effective C++学习笔记 chapter 2
Copyright © 2011-2022 走看看