0

我不能在 C 中使用 strset 函数。我正在使用Linux,并且我已经导入了 string.h 但它仍然不起作用。我认为Windows和Linux有不同的关键字,但我在网上找不到修复;他们都在使用Windows。

这是我的代码:

   char hey[100];
   strset(hey,'\0');   

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

^~~~~~ strsep

4

2 回答 2

5

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

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

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

char hey[100] = { 0 };  // Initialize all of the array to zero

如果您的目标是将现有的以空字符结尾的字符串“重置”为全零,请使用以下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

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

memset(hey, 0, strlen(hey));  // Set all of the string (but not including
                              // the null-terminator) to zero
于 2020-01-13T12:09:31.057 回答
1

strset不是标准的 C 函数。您可以使用标准功能memset。它有以下声明

void *memset(void *s, int c, size_t n);

例如

memset( hey, '\0', sizeof( hey ) );
于 2020-01-13T12:07:01.900 回答