我不断收到与类型有关的错误

问题描述 投票:0回答:1

我正在为家庭作业编写此程序,并且不断收到类型错误。据我所知%d读取整数,在这种情况下,变量x和%lf读取双精度“变量f”

我试图按照其他问题的要求在scanf()函数中删除“ \ n”

int x=0;
int l=0;
double f=0;
printf("Geben Sie eine ganze Zahl");
scanf("%d",x);
printf("Geben Sie eine reele Zahl");
scanf("%lf",f);
l=-1;
char r[1]="";
char s[1]="";
  while(l !=1){
    printf("Geben Sie ein Zeichen");
    scanf("%s",r);
    l=strlen(r);
}
return 0;

错误:

C:/Users/---(9): warning in format string of scanf(): the conversion %d expects type int* but given type is int (argument 1).
C:/Users/---(11): warning in format string of scanf(): the conversion %lf expects type double* but given type is double (argument 1).
c scanf
1个回答
3
投票

就像警告说的那样:该函数期望指向您提供的类型的指针。通过在变量前放置&来解决此问题,从而使其通过地址代替:

printf("Geben Sie eine ganze Zahl");
scanf("%d", &x);
printf("Geben Sie eine reele Zahl");
scanf("%lf", &f);

您在阅读字符时也遇到了问题。这个在这里

char r[1] = "";
char s[1] = "";

制作两个不包含空终止符的数组。它没有读取非空字符串的能力,您在此处执行此操作:

scanf("%s", r);

尚不清楚您是要读取整个字符串还是读取一个字符,正如输出所暗示的那样。对于一个字符,您的代码应如下所示:

char r;
scanf("%c", &r);

以及整个字符串:

char r[20]; // can hold 19 chars plus a null terminator
scanf("%19s", r);

调整这些大小以匹配您需要读取的字符串的长度。

© www.soinside.com 2019 - 2024. All rights reserved.