user5069419
user5069419

Reputation:

C Format '%d' expects argument of type 'int *' but argument 2 has type 'unsigned int' [-Wformat=]

I have some problem about this code , I want to compile this but it's not compile because something wrong. I told about wrong code down there...

#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;                                        
}

only int and float variables are not working while I was compile

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);
    ^

Please help me ://

Upvotes: 2

Views: 37461

Answers (3)

dbush
dbush

Reputation: 225767

The %d and %f format specifiers to scanf expect the address of an int and float respectively. Also release_year is an unsigned int, so the %u specifier should be used, and for rental_duration, which is a char, %hhd should be used.

So these:

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

Should be changed to:

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

You're not seeing an issue with %s because arrays -- when passed to a function -- decay to a pointer to the first element of the array.

EDIT:

Using the proper size specifier for scanf is of particular importance. If you were to use %d instead of %hhd for a char, scanf will attempt to write to a 4 byte location (assuming int is 32-bit) instead of to one byte, which will lead to undefined behavior.

From the man page:

   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.

Upvotes: 7

hexasoft
hexasoft

Reputation: 677

Obviously your problem comes from scanf("%d",rec[i].release_year);.

The scanf needs a pointer to your variable in order to be able to store de read value into it. So it must be: scanf("%d",&(rec[i].release_year));

Note: don't put several commands on the same line. Your tool pointed you to the line with the error, not to the function that raised it. If you had splitted this line into 3 (printf+printf+scanf) you would have seen faster where is the problem :)

Upvotes: 2

Barmar
Barmar

Reputation: 782727

You need to pass the address of the variable when you call scanf()

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

Upvotes: 4

Related Questions