c - 使用指针替换值

标签 c

这是我使用指针的第一个任务... 我需要创建一个函数来找出两个数字中哪个更大,而不是替换它们以获得更大的值并打印它们。

我在 main()prinf 中遇到错误:

argument type 'void' is incomplete

我的代码是:

#include <stdio.h>
void larger_of(double * x, double * y);

int main()

{
    double num1 = 4.5;
    double num2 = 5.5;

    printf("the original two numbers is %.1lf and %.1lf\n", num1, num2);
    printf("now: %lf and %lf", larger_of(&num1, &num2));

}

void larger_of(double * x, double * y)

{
    if (* x > * y)
        * y = * x;
    else if
       (* x < * y)
        * x = * y;
    else
        printf("they are equale!!");
}

最佳答案

larger_of 是一个不返回任何内容的函数。你不能打印它的返回值。

要在调用 larger_of 后打印您的号码,只需执行以下操作:

larger_of(&num1, &num2);
printf("now: %lf and %lf", num1, num2);

关于c - 使用指针替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14715159/

相关文章:

c++ - 信号量的 Peek 操作

c - 如何在ubuntu中使用C语言在.dat文件中追加数据?

c - 下面的表达式将如何在 C 中求值?

c - 排序内核linux链表

c - 在C中查找机器的IP地址?

c - 如何将 struct * 作为函数的参数

android - 设置 OpenGL ES 1.1 和我的 android 环境

c - 当将 char 指针指向的内存类型转换为无符号整数时,传入的值与预期不符

c - 如何将文件名添加到系统日志字符串?

c - 无法返回数组在 C 中实际上意味着什么?