如何在linux中使用c语言使用strset()

The*_*eee 0 c linux string string-function

我可以\xe2\x80\x99t在C中使用strset函数。我使用的是Linux,并且我已经导入了string.h,但它仍然不起作用。我认为Windows和Linux有不同的关键字,但我可以\xe2\x80\x99t在线找到修复程序;他们\xe2\x80\x99都使用Windows。

\n\n

这是我的代码:

\n\n
   char hey[100];\n   strset(hey,'\\0');   \n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

错误::警告:函数strset; did you\n meanstrsep` 的隐式声明?[-W隐式函数声明]\n strset(hey, '\\0');

\n\n

^~~~~~ strsep

\n
\n

Som*_*ude 5

首先strset(或者更确切地说_strset)是Windows特定的功能,它不存在于任何其他系统中。通过阅读其文档,它应该很容易实现。

但是,您还有第二个问题,因为您将未初始化的数组传递给函数,该函数需要一个指向以 null 结尾的字符串的第一个字符的指针。这可能会导致未定义的行为

解决这两个问题的方法是直接初始化数组:

char hey[100] = { 0 };  // Initialize all of the array to zero
Run Code Online (Sandbox Code Playgroud)

如果您的目标是将现有的空终止字符串“重置”为全零,则使用以下memset函数:

char hey[100];

// ...
// Code that initializes hey, so it becomes a null-terminated string
// ...

memset(hey, 0, sizeof hey);  // Set all of the array to zero
Run Code Online (Sandbox Code Playgroud)

或者,如果您想具体模拟以下行为_strset

memset(hey, 0, strlen(hey));  // Set all of the string (but not including
                              // the null-terminator) to zero
Run Code Online (Sandbox Code Playgroud)