我想學習使用utmp.h自帶的函式和資料結構。在下面的代碼中,我想遍歷 utmp 結構并列印它們的資料欄位。
#include <stdio.h>
#include <utmp.h>
int main()
{
struct utmp *data;
data = getutent();
int i = 0 ;
while(data != NULL)
{
i;
printf("%s\n" , data->ut_id);
data = getutent();
}
printf("%d" , i);
return 0 ;
}
即使ut_id是 type char[4],當我運行代碼時,我也會收到此警告:
警告:'__builtin_puts' 引數 1 宣告屬性 'nonstring' [-Wstringop-overflow=]
我該如何解決?
uj5u.com熱心網友回復:
此警告是 gcc 特定屬性的結果,該屬性__attribute_nonstring__用作指示字符陣列不一定以NUL字符結尾的指示符,因此與標準庫字串函式一起使用可能不安全。utmpLinux 中的結構在其字符陣列欄位上使用該屬性定義。
要解決該警告,您可以使用指定字符陣列固定寬度輸出的printf()修飾符%.*s,如下所示:
printf("%.*s\n" , (int)(sizeof data->ut_id), data->ut_id);
(您可以只4用于第二個引數,但sizeof更靈活)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335259.html
上一篇:保護不透明資料型別的資料
下一篇:如何解決一個模式?
