2015-10-15 253 views
1

我對這段代碼有一些問題,我想編譯它,但它不是編譯的,錯誤。我告訴錯誤的代碼那裏......C格式'%d'需要類型爲'int *'的參數,但參數2的類型爲'unsigned int'[-Wformat =]

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define MAX 1000         
typedef struct{ 
    int film_id; 
    char title[255]; 
    char description[1023]; 
    unsigned int release_year; 
    char rental_duration; 
    float rental_rate; 
    unsigned char length; 
    float replacement_cost; 
    char rating[10]; 
    char last_update[30]; 
} RECORD_t, *RECORD;         

int main(){ 
    int i,R1; 
    char R[1]; 
    RECORD rec = (RECORD)malloc(sizeof(RECORD_t)*MAX); 
    FILE *file = fopen("data.txt", "rb");    
    if (file == NULL) {         
     printf("Cannot open the file.\n");   
     exit(0);          
    }             
    fread(rec, sizeof(RECORD_t)*MAX, 1, file);   
    fclose(file);          

printf("İd Numarası Giriniz : \n"); 
     scanf("%d",&R1); 
     for (i=0; i<1000; i++){ 
      if (R1 == rec[i].film_id) { 

      printf("TITLE: %s\n", rec[i].title); printf("Enter New TITLE : "); scanf("%s",rec[i].title); 
      printf("DESCRIPTION: %s\n", rec[i].description); printf("Enter New Description : "); scanf("%s",rec[i].description); 

// My problem is this line :------------------- 
      printf("RELEASE YEAR: %d\n", rec[i].release_year); printf("Enter New RELEASE YEAR : "); scanf("%d",rec[i].release_year); 
      printf("RENTAL DURATION: %d\n", rec[i].rental_duration); printf("Enter New RENTAL DURATION : "); scanf("%d",rec[i].rental_duration); 
      printf("RENTAL RATE: %f\n", rec[i].rental_rate); printf("Enter New RENTAL RATE : "); scanf("%f",rec[i].rental_rate); 
      printf("REPLACEMENT COST: %f\n", rec[i].replacement_cost); printf("Enter New REPLACEMENT COST : "); scanf("%f",rec[i].replacement_cost); 
      printf("RATING: %s\n", rec[i].rating); printf("Enter New RATING : "); scanf("%s",rec[i].rating); 
      printf("LAST UPDATE: %s\n", rec[i].last_update); printf("Enter New LAST UPDATE : "); scanf("%s",rec[i].last_update); 

      } 
     } 

    } 


    file = fopen("data.txt", "wb");     
    fwrite(rec, sizeof(RECORD_t)*MAX, 1, file);  
    fclose(file);          
    free(rec);          
    return 1;           
} 

只有int和float變量不工作,而我是編譯

warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned int’ [-Wformat=] 
    printf("RELEASE YEAR: %d\n", rec[i].release_year); printf("Enter New RELEASE YEAR : "); scanf("%d",rec[i].release_year); 
    ^

請大家幫幫我://

+0

1)'的scanf( 「%s」 時,REC [I] .title僞);'不能防止過分解讀'title'。最好使用'scanf(「%254s」,rec [i] .title);'2)'scanf(「%s」,rec [i] .title);'不能用空格3 )推薦'memset(rec [i] .title,0,sizeof rec [i] .title);'在讀入標題之前清理隨機數據的字段 - 在調試時沒有垃圾記錄時很有用。 – chux

+0

將格式字符串中的'%d'更改爲'%u',以匹配無符號數字。 –

回答

4

你需要傳遞當你調用scanf()

scanf("%d", &rec[i].release_year); 
+0

之後立即與'rental_duration','rental_rate'和'replacement_cost'相同。 –

+0

非常感謝你:)) – oEs

+1

他還必須使用'unsigned'格式說明符。 – Olaf

2

變量的地址,顯然你的問題來自scanf("%d",rec[i].release_year);

scanf需要一個指向你的變量的指針,以便能夠將de讀值存儲到它的變量中。所以它必須是:scanf("%d",&(rec[i].release_year));

注意:不要把幾個命令放在同一行。您的工具將您指向錯誤的行,而不是引發它的函數。如果你已經分裂這一行到3(的printf +的printf scanf的+),你會看到更快的問題出在哪裏:)

+0

他還必須使用'unsigned'格式說明符。 – Olaf

+0

如果我記得好,它會導致警告,而不是錯誤,它會起作用(並不總是如預期的那樣,但是...)。但是,是的,你是對的。 – hexasoft

+0

無論給出何種類型的組合,IIRC都會將報告的format-sting/variadic參數錯誤報告爲警告。 – Olaf

6

%d%f格式說明scanf期待的分別intfloat地址。另外release_yearunsigned int,所以應該使用%u說明符,並且應該使用rental_duration(這是char%hhd)。

所以這些:

scanf("%d",rec[i].release_year); 
scanf("%d",rec[i].rental_duration); 
scanf("%f",rec[i].rental_rate); 
scanf("%f",rec[i].replacement_cost); 

應改爲:

scanf("%u",&rec[i].release_year); 
scanf("%hhd",&rec[i].rental_duration); 
scanf("%f",&rec[i].rental_rate); 
scanf("%f",&rec[i].replacement_cost); 

你沒有看到的問題與%s因爲數組 - 當傳遞給函數 - 衰變爲指針到數組的第一個元素。

編輯:

使用適當的大小說明符scanf是特別重要的。如果您要使用%d而不是%hhd代替char,scanf將嘗試寫入4個字節的位置(假設int爲32位)而不是一個字節,這將導致未定義的行爲。

從手冊頁:

h  Indicates that the conversion will be one of diouxX or n and the 
      next pointer is a pointer to a short int or unsigned short int 
      (rather than int). 

    hh  As for h, but the next pointer is a pointer to a signed char or 
      unsigned char. 

    l  Indicates either that the conversion will be one of diouxX or n 
      and the next pointer is a pointer to a long int or unsigned long 
      int (rather than int), or that the conversion will be one of efg 
      and the next pointer is a pointer to double (rather than float). 
      Specifying two l characters is equivalent to L. If used with %c 
      or %s the corresponding parameter is considered as a pointer to 
      a wide character or wide character string respectively. 

    L  Indicates that the conversion will be either efg and the next 
      pointer is a pointer to long double or the conversion will be 
      dioux and the next pointer is a pointer to long long. 
+0

@Olaf謝謝。固定,再加上一個。 – dbush

+0

嗯......如果'char'是無符號的呢?沒有'signed' /'unsigned'的IMO'char'確實應該只用於字符。 – Olaf

+0

'%hhu'可用於'unsigned char'。我同意,雖然使用'char'進行數字輸入可能會讓讀者誤以爲它的典型用法。 – dbush

相關問題