#include <stdio.h>
#include <ctype.h>
int main(void) {
int j;
scanf ("%d",&j);
if (j>=1&&j<=10){
int i=0;
int USER_NAME=100;
char name[j][USER_NAME];
for (i=0;i<j;i ){
scanf ("%s",name[i]);
}
for (i=0;i<j;i ){
if ((i%2)==0){
i ;
}
if ((i%2)!=0){
printf ("%s\n",name[i]);
}
}
}
else (printf ("No additional constrainsts"));
return 0;
}
它不斷給我這個錯誤
./oddecho.c: In function 'main':
./oddecho.c:6:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
6 | scanf ("%d",&j);
| ^~~~~~~~~~~~~~~
./oddecho.c:12:7: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
12 | scanf ("%s",name[i]);
| ^~~~~~~~~~~~~~~~~~~~
我嘗試在第 6 行中的 scanf 之前使用 (void) 但它仍然繼續竊聽。有人可以幫助為什么這個錯誤不斷彈出嗎?任何幫助,將不勝感激
uj5u.com熱心網友回復:
通常,您需要檢查 的回傳值scanf以確保輸入成功。
警告說明了這一點。
為避免警告,您可以將運算式scanf轉換為 void 型別
( void )scanf ("%d",&j);
請注意,由于在此 for 回圈中將變數 i 增加了兩次
for (i=0;i<j;i ){
if ((i%2)==0){
i ;
}
if ((i%2)!=0){
printf ("%s\n",name[i]);
}
}
它什么也不能輸出。:)
寫就好了
for (i=0;i<j;i ){
if ((i%2)!=0){
printf ("%s\n",name[i]);
i ;
}
}
uj5u.com熱心網友回復:
我在你的代碼中發現了一些問題。
- 您正在使用非常量變數來分配靜態陣列。IDK如何在你的編譯器編譯它沒有給出一個錯誤,
j而USER_NAME不是常量。我建議改用此代碼:
...
// Manual allocate memories
char** names = (char**)malloc(j * sizeof(char*));
for (i = 0; i < j; i ) {
names[i] = (char*)malloc(USER_NAME * sizeof(char));
scanf("%s", names[i]);
}
...
// Free memories
for (i = 0; i < j; i ) {
free(names[i]);
}
free(names);
...
- 如果您遇到
error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.由 MSVC 編譯器引起的錯誤,您可以在包含標題之前放置這一行#define _CRT_SECURE_NO_WARNINGS或查看此參考https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning -level-3-c4996?view=msvc-170了解更多詳情。 - 關于警告。scanf 函式的簽名是
int scanf ( const char * format, ... ),這意味著它回傳一個整數值。使用它而不存盤從它回傳的值可能會導致該警告。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/385482.html
