【发布时间】:2015-12-08 22:58:21
【问题描述】:
我一直在研究一种应该将 Pascal 字符串转换为 C 字符串的方法。我还被告知返回的char * 应该指向一个新分配的char 数组,其中包含一个以空字符结尾的C 字符串。被调用者负责在这个数组上调用free()。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *pascal_convert(void *x)
{
int *y;
x = y;
char *z;
*z = *((int*)x);
char *arr = malloc(sizeof(*z));
for (int i = 0; i < *y; i++)
{
arr[i] = z[i];
}
char* fin = arr;
return fin;
}
【问题讨论】:
-
pascal_convert中有哪些错误?你测试了吗? -
为什么 x= y 而不是 y = x?
-
我会说“调用者”负责调用
free()。