- 论坛徽章:
- 780
|
本帖最后由 Herowinter 于 2014-01-03 12:59 编辑
回复 4# asker123
比如,像下面的代码,没有这一句,
strlen()返回38而不是正确值35。- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void json_escape(char *str)
- {
- size_t size = strlen(str)+1;
- char *tmp = (char *)malloc(size*sizeof(char));
- strcpy(tmp,str);
- char *found = tmp;
- while (*found != '\0')
- {
- if(*found == '\\')
- {
- *str++ = '\\';
- }
- *str++ = *found++;
- }
- //*str='\0';
- free(tmp);
- }
- int main()
- {
- char str[] = "C:\\ProgramFiles\\Windows\\System32";
- printf("Before: %s\n",str);
- printf("strlen: %d\n",strlen(str));
- json_escape(str);
- printf("After: %s\n",str);
- printf("Strlen: %d\n", strlen(str));
- return 0;
- }
复制代码 |
|