c - 使用指针的程序运行不正常

Closed. This question needs to be more focused。它当前不接受答案。












想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。

3个月前关闭。



Improve this question




当您从键盘输入三个数字时,将计算并显示它们的平均值,并显示输入值本身以及从输入值中减去平均值的结果(带+号,最多三位小数)。程序来做。
规范:
  • 在键盘上输入并显示结果。
    主面。输入的值是浮点数。
  • 在函数ave3()中计算平均值。这个
    函数从主要方面接受三个数据作为参数,
    计算它们的平均值,并将结果返回
    值。
  • 从输入数据中减去平均值的操作
    在功能sub3()中执行。该功能接收
    有关来自主要方面和平均值的三个数据的信息
    先前计算为参数的值,然后减去平均值
    从这三个数据中的每一个。此函数没有返回值,并且
    主端输出在函数中更改的三个值
    他们是。

  • 源代码
    #include <stdio.h>
    
    double ave3 (double, double, double);
    double subave3 (double *, double *, double *);
    
    int main ()
    {
       double a, b, c, ave;
    
       printf ("Please enter three values: \ n");
       scanf ("% lf% lf% lf", & a, & b, & c);
    
       ave = ave3 (& a, & b, & c); / * Function call * /
    
       printf ("Average:% .3f \ n", ave);
       printf ("Original data:% + .3f% + .3f% + .3f \ n", a, b, c);
    
       subave3 (double * x, double * y, double * z);
    
       printf ("Data after average deduction:% + .3f% + .3f% + .3f \ n", a, b, c);
    
       return 0;
    }
    
    / * A function that calculates the average. It also subtracts the average value from the data. * /
    double subave3 (double x, double y, double z)
    {
       x-= ave; / * Subtract the average from each data * /
       y-= ave;
       z-= ave;
    
       a = x;
       b = y;
       c = z;
    
       return a, b, c;
    }
    
    double ave3 (double x, double y, double z) {
       ave = (* x + * y + * z) / 3.0;
       return ave;
    }
    
    错误
    prog01.c: In function ‘main’:
    prog01.c: 13: 14: error: incompatible type for argument 1 of ‘ave3’
       13 | ave = ave3 (& a, & b, & c); / * Function call * /
          | ^ ~
          | |
          | double *
    prog01.c: 3:13: note: expected ‘double’ but argument is of type ‘double *’
        3 | double ave3 (double, double, double);
          | ^ ~~~~~
    prog01.c: 13: 18: error: incompatible type for argument 2 of ‘ave3’
       13 | ave = ave3 (& a, & b, & c); / * Function call * /
          | ^ ~
          | |
          | double *
    prog01.c: 3:21: note: expected ‘double’ but argument is of type ‘double *’
        3 | double ave3 (double, double, double);
          | ^ ~~~~~
    prog01.c: 13: 22: error: incompatible type for argument 3 of ‘ave3’
       13 | ave = ave3 (& a, & b, & c); / * Function call * /
          | ^ ~
          | |
          | double *
    prog01.c: 3:29: note: expected ‘double’ but argument is of type ‘double *’
        3 | double ave3 (double, double, double);
          | ^ ~~~~~
    prog01.c: 18:11: error: expected expression before ‘double’
       18 | subave3 (double * x, double * y, double * z);
          | ^ ~~~~~
    prog01.c: 18: 3: error: too few arguments to function ‘subave3’
       18 | subave3 (double * x, double * y, double * z);
          | ^ ~~~~~~
    prog01.c: 4: 8: note: declared here
        4 | double subave3 (double *, double *, double *);
          | ^ ~~~~~~
    prog01.c: At top level:
    prog01.c: 26: 8: error: conflicting types for ‘subave3’
       26 | double subave3 (double x, double y, double z)
          | ^ ~~~~~~
    prog01.c: 4: 8: note: previous declaration of ‘subave3’ was here
        4 | double subave3 (double *, double *, double *);
          | ^ ~~~~~~
    prog01.c: In function ‘subave3’:
    prog01.c: 28: 8: error: ‘ave’ undeclared (first use in this function); did you mean ‘ave3’?
       28 | x-= ave; / * Subtract the average from each data * /
          | ^ ~~
          | ave3
    prog01.c: 28: 8: note: each undeclared identifier is reported only once for each function it appears in
    prog01.c: 32: 3: error: ‘a’ undeclared (first use in this function)
       32 | a = x;
          | ^
    prog01.c: 33: 3: error: ‘b’ undeclared (first use in this function)
       33 | b = y;
          | ^
    prog01.c: 34: 3: error: ‘c’ undeclared (first use in this function)
       34 | c = z;
          | ^
    prog01.c: In function ‘ave3’:
    prog01.c: 40: 3: error: ‘ave’ undeclared (first use in this function); did you mean ‘ave3’?
       40 | ave = (* x * y * z) / 3.0;
              | ^ ~~
          | ave3
    prog01.c: 40:10: error: invalid type argument of unary ‘*’ (have ‘double’)
       40 | ave = (* x * y * z) / 3.0;
                                     | ^ ~
    prog01.c: 40: 15: error: invalid type argument of unary ‘*’ (have ‘double’)
       40 | ave = (* x * y * z) / 3.0;
                                     | ^ ~
    prog01.c: 40: 20: error: invalid type argument of unary ‘*’ (have ‘double’)
       40 | ave = (* x * y * z) / 3.0;
                                            | ^ ~
    

    最佳答案

    函数ave需要三个double参数,例如abc。但是,通过获取他们的地址,以下传递了double *参数:

    ave = ave3 (& a, & b, & c);
    
    只需将其更改为:
    ave = ave3 (a, b, c);
    
    逐一查看参数,并将您要传递的内容与函数期望的类型进行比较,并确保它们兼容。
    另外,以下操作将无效:
    subave3 (double * x, double * y, double * z);
    
    这是一个函数调用,而不是声明。将其更改为:
    subave3 (a, b, c);
    
    这可能不是您想要的,但是会编译。
    subave3中,即使未在函数中声明abc,它也尝试引用。如果要返回这些值,则需要传递它们的地址并通过指针存储这些值,因为在return语句中不能返回多个值。
    ave3中,由于abc不是指针,因此以下操作无效:
    ave = (* x + * y + * z) / 3.0;
    
    另外,未声明ave。更改为:
    double ave = (x + y + z) / 3.0;
    
    可能存在其他问题,并且在任何情况下都可能需要重新考虑。只需记住,将&应用于类型t的变量将得到类型为t *的指针,而将*应用于类型t *的表达式将得到类型为t的值。

    https://stackoverflow.com/questions/64460994/

    上一篇:delphi - delphi编译错误 "[DCC Error] ProjectName.dpr([number]): E1026 File not found: ' FileName.dn'

    下一篇:python - mysql-python的pip安装错误无法正常工作

    相关文章:

    asp.net-mvc - 如何为采用参数的方法配置Fluent安全策略?

    c++ - 加密货币在编译时出现错误

    vb.net - Visual Basic 编译器无法从以下错误中恢复 : System Error &Hc0000005& (Visual Basic internal compiler error)

    c++ - 当 T = int& 时,为什么构造函数 Message(const T& data) 与 Message(T&& data) 冲突?

    c - 从类型 ‘memstruct’ 分配给类型 ‘int’ 时的类型不兼容

    c - 初等C题

    c - 我将如何使用此 void 函数来计算百分比?

    c - 字母字符打印错误的 ISO 代码

    c - alloca inside 复合语句

    c++ - CppCheck。可以缩小变量的范围(和循环)

    相关文章:

    c++ - 我的程序未发现struct中的功能

    c# - C#中的编译时错误

    c++ - LNK2019未解析的外部符号。创建二叉树

    python - 使用 MinGW+GCC 编译 SciPy 时编译器失败

    c++ - 在此范围C++中未声明错误 'menu'

    .net - 使用xmlrpc和vb.net在wordpress中发布错误

    php - PHP中 undefined index 错误

    delphi - delphi编译错误 "[DCC Error] ProjectName.dpr([

    c++ - 代码编译错误

    c# - 如何正确地将 Dictionary,Object