码迷,mamicode.com
首页 > 编程语言 > 详细

浅谈C语言字符串结束符'\0'

时间:2018-07-18 17:03:17      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   pre   htm   情况   ida   target   log   color   结束   

如果你希望你的字符串以’\0‘结束,那么你可以这样做:

1 char str[]={"hello"};//①字符串赋值
2 char str[]={h,e,l,l,o,‘\0};//②人为添加
3 char str[6]={h,e,l,l,o};//③故意给数组预留一个空位

注:当出现以下情况时,会发生‘\0‘丢失

1 char str[5]={"hello"};//①数组长度不够
2 char str[]={h,e,l,l,o};//②不指定数组长度,把每个字符单独用引号括起来

使用函数strlen()求某个字符串的长度时是不包括结尾标志符‘\0‘的,但当你用sizeof()求某个字符串占用的内存空间时,结尾字符‘\0‘是被包括在里面的。

参考:

  1.http://www.cnblogs.com/kaituorensheng/archive/2013/12/09/3464462.html

  2.https://zhidao.baidu.com/question/42473019.html

浅谈C语言字符串结束符'\0'

标签:style   pre   htm   情况   ida   target   log   color   结束   

原文地址:https://www.cnblogs.com/stefango/p/9329405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!