C语言 C 格式 '%d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'unsigned int' [-Wformat=]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33157946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C Format '%d' expects argument of type 'int *' but argument 2 has type 'unsigned int' [-Wformat=]
提问by hexasoft
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
我编译时只有 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);
^
Please help me ://
请帮我 ://
回答by dbush
The %dand %fformat specifiers to scanfexpect the address of an intand floatrespectively. Also release_yearis an unsigned int, so the %uspecifier should be used, and for rental_duration, which is a char, %hhdshould be used.
在%d与%f格式说明scanf预期的地址int和float分别。还release_year是一个unsigned int,所以%u应使用说明符,并为rental_duration,这是一个char,%hhd应该被使用。
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 %sbecause arrays -- when passed to a function -- decay to a pointer to the first element of the array.
您没有看到问题,%s因为数组——当传递给函数时——衰减到指向数组第一个元素的指针。
EDIT:
编辑:
Using the proper size specifier for scanfis of particular importance. If you were to use %dinstead of %hhdfor a char, scanfwill attempt to write to a 4 byte location (assuming intis 32-bit) instead of to one byte, which will lead to undefined behavior.
使用适当的大小说明符scanf特别重要。如果您要使用%d而不是%hhdfor a char,scanf将尝试写入 4 字节位置(假设int是 32 位)而不是写入 1 个字节,这将导致未定义的行为。
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.
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.
回答by Barmar
You need to pass the address of the variable when you call scanf()
调用时需要传递变量的地址 scanf()
scanf("%d", &rec[i].release_year);
回答by hexasoft
Obviously your problem comes from scanf("%d",rec[i].release_year);.
显然你的问题来自scanf("%d",rec[i].release_year);.
The scanfneeds 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));
在scanf为了需要的一个指针变量能够去读取值存储到它。所以它必须是:scanf("%d",&(rec[i].release_year));
Note: don'tput 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 :)
注意:不要将多个命令放在同一行。您的工具将您指向错误所在的行,而不是引发错误的函数。如果您将此行拆分为 3 (printf+printf+scanf),您会更快地看到问题出在哪里:)

