为什么sscanf()不能将字符串写入char* s中?我将其初始化为NULL,因为我不想使用未初始化的变量。
#include <stdio.h>
#include <string.h>
int main()
{
char* t = "I am a monkey";
char *s = NULL;
sscanf(t, "%s",s);
printf("%s\n",s);
}
NULL
的含义。 - juanchopanzachar *s = malloc(16)
来代替char *s = NULL
。(需要包含头文件#include <stdlib.h>
),然后再使用printf("%s\n", s); free(s);
。 - BLUEPIXYsscanf(t, "%ms", &s);
(记得释放s
内存:free(s)
)。 - BLUEPIXYs
的副本。它无法更改s
的值。如果您仍然感到困惑,请查看按值传递的相关资料。 - Dylan Kirkby