未更改scanf的第二个值

未更改scanf的第二个值,c,C,当&添加到y时,y不会更改,但是如果我删除&,y会更改,但程序会在scanf之后立即结束: double scan_data(char *a, double *b) { char x; double y; int result; printf("input en operator og hvis relevant, operand: "); scanf("%c, %lf", &x, &y);

&
添加到
y
时,
y
不会更改,但是如果我删除
&
y
会更改,但程序会在
scanf
之后立即结束:

double scan_data(char *a, double *b) {
    char x;
    double y;
    int result;
    printf("input en operator og hvis relevant, operand: ");
    scanf("%c, %lf", &x, &y);
    /* Hvis unær output=0,0. */
    printf("scan y step 1: %lf", &y);
    unary(x, &result);
    if (result == -1) {
        y = 0.0;
    }
    *a = x;
    *b = y;
    printf("scan b: %d", *b);
    return 0;
}
调用未定义的行为,因为具有错误类型的数据被传递到
printf()
%lf
需要
double
,但
&y
具有类型
double*

应该是

printf("scan y step 1: %f", y);
您应该删除额外的
&
。另外,应使用
%f
printf()
中打印
double
。(
%lf
在C99或更高版本中还不错)

还调用未定义的行为,因为
double
被传递到需要
int
的位置

应该是

printf("scan b: %f", *b);

调用未定义的行为,因为具有错误类型的数据被传递到
printf()
%lf
需要
double
,但
&y
具有类型
double*

应该是

printf("scan y step 1: %f", y);
您应该删除额外的
&
。另外,应使用
%f
printf()
中打印
double
。(
%lf
在C99或更高版本中还不错)

还调用未定义的行为,因为
double
被传递到需要
int
的位置

应该是

printf("scan b: %f", *b);


scanf()
的参数必须是
scanf()
存储转换值的地址。如果将
y
定义为
double
,则必须传递
&y
y
的地址)进行转换
%lf
,该转换需要指向
double
的指针

对于
printf
,必须为
%f
(或
%lf
,其中
l
被忽略)传递一个
double
值。传递
&y
是错误的,只要传递
y

最后的
printf
也有一个类型不匹配:在
printf中(“扫描b:%d”,*b)
%d
需要一个
int
值,但
*b
具有类型
双精度
。你应该写:

printf("scan b: %f\n", *b);
以下是修改后的代码:

double scan_data(char *a, double *b) {
    char x;
    double y;
    int result;
    printf("input en operator og hvis relevant, operand: ");
    if (scanf("%c, %lf", &x, &y) != 2) {
        printf("invalid input\n");
        return -1.0;
    }
    /* Hvis unær output=0,0. */
    printf("scan y step 1: %f\n", y);

    unary(x, &result);  // no info on this function?

    if (result == -1) {
        y = 0.0;
    }
    *a = x;
    *b = y;
    printf("scan b: %f\n", *b);
    return 0.0;
}

scanf()
的参数必须是
scanf()
存储转换值的地址。如果将
y
定义为
double
,则必须传递
&y
y
的地址)进行转换
%lf
,该转换需要指向
double
的指针

对于
printf
,必须为
%f
(或
%lf
,其中
l
被忽略)传递一个
double
值。传递
&y
是错误的,只要传递
y

最后的
printf
也有一个类型不匹配:在
printf中(“扫描b:%d”,*b)
%d
需要一个
int
值,但
*b
具有类型
双精度
。你应该写:

printf("scan b: %f\n", *b);
以下是修改后的代码:

double scan_data(char *a, double *b) {
    char x;
    double y;
    int result;
    printf("input en operator og hvis relevant, operand: ");
    if (scanf("%c, %lf", &x, &y) != 2) {
        printf("invalid input\n");
        return -1.0;
    }
    /* Hvis unær output=0,0. */
    printf("scan y step 1: %f\n", y);

    unary(x, &result);  // no info on this function?

    if (result == -1) {
        y = 0.0;
    }
    *a = x;
    *b = y;
    printf("scan b: %f\n", *b);
    return 0.0;
}

你真的用逗号输入了输入吗?请看。我刚才试过了,但遗憾的是它没有起到任何作用。您的问题是什么?请包括一个,以及您提供的完整和实际的复制粘贴输入。您需要提供例如
+,5
,作为输入。格式字符串中的逗号必须在实际输入中匹配。您是否确实在输入之间键入了逗号?请看。我刚才试过了,但遗憾的是它没有起到任何作用。您的问题是什么?请包括一个,以及您提供的完整和实际的复制粘贴输入。您需要提供例如
+,5
,作为输入。格式字符串中的逗号必须在实际输入中匹配。对不起,我是c的新手。为什么双y是双*的类型?另外,如果我删除&程序就在扫描之后结束:/
y
具有类型
double
。使用一元
&
运算符,可以获取指向值的指针。指向
double
的指针类型是
double*
,因此
&y
的类型是
double*
。另外,您不应该删除
scanf()
的参数中的
&
,因为扫描东西需要指针。为什么双y是双*的类型?另外,如果我删除&程序就在扫描之后结束:/
y
具有类型
double
。使用一元
&
运算符,可以获取指向值的指针。指向
double
的指针类型是
double*
,因此
&y
的类型是
double*
。另外,您不应该在
scanf()
的参数中删除
&
,因为扫描东西需要指针。我从printf中删除了&并最终成功,但是如果我从%lf中删除了l,问题仍然存在。@LukasJensen:
scanf()中需要的
l
format string和
printf
格式字符串中的可选项。我从printf中删除了&并最终成功,但是如果我从%lf中删除了l,问题仍然存在。@LukasJensen:
scanf()
格式字符串中需要的
l
printf
格式字符串中的可选项。