我正在尝试创建一个返回 int 的 C 函数,但在此过程中将填充作为变量传入的 char*。我正在尝试的一个基本示例是:
int myMethod(int input, char* output, int outMaxLen) {
int outValue = input * 5;
if (out < 10) strcat(output, "A small number");
else if (out < 20) strcat(output, "A medium number");
else strcat(output, "A large number");
}
在 main.c 中:
char* myMethodOutput;
int myMethodInt = myMethod(2, myMethodOutput, 15);
printf("%d %s", myMethodInt, myMethodOutput);
运行时,整数显示在屏幕上,但文本不显示。
outMaxLen 变量用于检查 char* 参数以确保它足够大以容纳输出字符串。
除了 strcat(),我还尝试了 strcpy() 和 strncpy(),但都无济于事。strcat() 不向控制台显示任何文本,并且 strcpy() 和 strncpy() 使用消息 EXC_BAD_ACCESS 调用调试器。
我已经通过使用 strcpy_s 函数在 Windows API 中成功地管理了这个,但我现在正在尝试使用 UNIX 机器。我可能错过了一些非常基本的东西!