[英]How to use strset() in linux using c language
我不能在 C 中使用 strset 函數。我正在使用Linux ,並且我已經導入了 string.h 但它仍然不起作用。 我認為 Windows 和 Linux 有不同的關鍵字,但我在網上找不到修復程序; 他們都在使用 Windows。
這是我的代碼:
char hey[100];
strset(hey,'\0');
錯誤::警告:函數
strset; did you mean
隱式聲明strset; did you mean
strset; did you mean
strsep 嗎? [-Wimplicit-function-declaration] strset(hey, '\\0');^~~~~~~ strsep
首先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
strset
不是標准的 C 函數。 您可以使用標准函數memset
。 它有以下聲明
void *memset(void *s, int c, size_t n);
例如
memset( hey, '\0', sizeof( hey ) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.