- 论坛徽章:
- 0
|
本帖最后由 arserangel 于 2012-07-26 16:36 编辑
- PHP_FUNCTION(self_concat)
- char *str = NULL;
- int argc = ZEND_NUM_ARGS();
- int str_len;
- long n;
- char *result; /* Points to resulting string */
- char *ptr; /* Points at the next location we want to copy to */
- int result_length; /* Length of resulting string */
- if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
- return;
- }
- /* Calculate length of result */
- result_length = (str_len * n);
- /* Allocate memory for result */
- result = (char *) emalloc(result_length + 1);
- /* Point at the beginning of the result */
- ptr = result;
- while (n--) { /* Copy str to the result */
- memcpy(ptr, str, str_len); /* Increment ptr to point at the next position we want to write to */
- ptr += str_len;
- }
- /* Null terminate the result. Always null-terminate your strings even if they are binary strings */
- *ptr = '/0';
- /* Return result to the scripting engine without duplicating it*/
- RETURN_STRINGL(result, result_length, 0);
- }
复制代码 两个字符串拷贝,必须要有一个中间变量 ptr ?
*ptr = '\0'; 是在字符串抹尾添加一个 \0 结束标识符么?
刚开始学 c 见笑了。 |
|