某些 C 函数可以修改字符串值,而无需我传递它的地址。 例如:
char * s = "foo\0";
tobaa(s); /* Now foo is modified to something like "baa" */
printf("%s", s); /* baa */
我该怎么做?
最佳答案
简单:
void tobaa(char *s)
{
strcpy(s, "baa");
}
此外,额外的 \0
字符是不必要的,它是自动添加的。
此外,确保将可变缓冲区传递给 tobaa
或调用 UB。
关于c - 如何在不传递 char ** 作为参数的情况下修改字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956502/