[英]Concatenating char string in C
我搜寻了互联网无济于事。 我不明白这个问题在问什么。
void case_three(int x, int y, char *actualResult) {
int i, j, s, t, p, q;
s = i = x; // initialize variables with value from x
t = j = y; // initialize variables with value from y
p = func(++i, ++j);
q = mac(++s, ++t);
// Copy the output to actualResult below...
printf("\n\n"); //first variable increment
printf("Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
// Replace the quoted content in the following strcpy statement with the actual output from last printf statement above.
// Do not alter the text or add any spaces to it.
strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q);
printf("\n\n");
printf(actualResult);
}
当我在VS中运行代码时,我得到的func
和mac
解决方案分别为1和1。 当我打印actualResult
字符串时,我得到的巨大数字每次执行时都会改变。 另外,当我尝试在gcc中进行编译时,我得到一个error: too many arguments to function strcpy
在strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q);
线。
因此,我需要将输出从printf
函数复制到char字符串actualResult
但不知道如何正确执行。
任何帮助表示赞赏。
非常简单:使用“ sprintf()”代替“ printf()”将格式化输出输出到字符串。
您不需要“ strcpy()”, 也不能将 strcpy与格式化命令一起使用。
例:
/* The exact same output will go to your terminal as to the string "actualResult" */
printf("Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
sprintf(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.