免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1874 | 回复: 6
打印 上一主题 下一主题

[C] c语言 基本问题求教: *ptr = '/0'; [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-26 16:34 |只看该作者 |倒序浏览
本帖最后由 arserangel 于 2012-07-26 16:36 编辑

  1. PHP_FUNCTION(self_concat)
  2.     char *str = NULL;
  3.     int argc = ZEND_NUM_ARGS();
  4.     int str_len;

  5.     long n;
  6.     char *result; /* Points to resulting string */
  7.     char *ptr; /* Points at the next location we want to copy to */
  8.     int result_length; /* Length of resulting string */

  9.     if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
  10.         return;
  11.     }

  12.     /* Calculate length of result */
  13.     result_length = (str_len * n);
  14.     /* Allocate memory for result */
  15.     result = (char *) emalloc(result_length + 1);
  16.     /* Point at the beginning of the result */

  17.     ptr = result;

  18.     while (n--) { /* Copy str to the result */
  19.         memcpy(ptr, str, str_len); /* Increment ptr to point at the next position we want to write to */
  20.         ptr += str_len;
  21.     }

  22.     /* Null terminate the result. Always null-terminate your strings even if they are binary strings */
  23.     *ptr = '/0';

  24.     /* Return result to the scripting engine without duplicating it*/

  25.     RETURN_STRINGL(result, result_length, 0);

  26. }
复制代码
两个字符串拷贝,必须要有一个中间变量 ptr ?
*ptr = '\0'; 是在字符串抹尾添加一个 \0 结束标识符么?

刚开始学 c 见笑了。

论坛徽章:
0
2 [报告]
发表于 2012-07-26 19:46 |只看该作者
串后边添加一个 '\0';

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
3 [报告]
发表于 2012-07-26 20:41 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2012-07-26 20:42 |只看该作者
加一个呗。

论坛徽章:
0
5 [报告]
发表于 2012-07-27 13:37 |只看该作者
是 *p='\0'; 不是 '/0'

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
6 [报告]
发表于 2012-07-27 14:38 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
7 [报告]
发表于 2012-07-28 15:37 |只看该作者
这是我看的别人写的 php 扩展里的代码, 确实有误, /0 应该为 \0 才对。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP