使用函数从int *到int的转换无效

时间:2015-12-10 21:29:44

标签: c function variables

我有这个"简单"问题:我输入2个int数字,我必须按降序输出它们。

#include <stdio.h>
#include <iostream>

int fnum()
{
    int NUM;
    scanf("%d",&NUM);
    return NUM;
}

void frisultato(int x,int y)
{
    if (x>y)
    {
            printf("%d",x);
            printf("%d",y);
    }
    else
    {
            printf("%d",y);
            printf("%d",x);
    }

    return;
}

int main()
{    
      int A,B;     
      A=fnum;
      B=fnum;

      frisultato(A,B);
}

我在

收到错误
A=fnum;
B=fnum;

我的编译器说:从int(*)()无效转换为int。

这是我第一次使用功能,有什么问题?谢谢!

  • 米开朗基罗。

2 个答案:

答案 0 :(得分:3)

  A=fnum;
  B=fnum;

您实际上并未在此处调用函数fnum。您正在尝试将指向该函数的指针分配给int变量AB

要调用该函数,请执行以下操作:

  A=fnum();
  B=fnum();

答案 1 :(得分:0)

很抱歉,但由于您似乎是编程新手,我无法帮助您重构/评论您的代码:

#include <stdio.h>
#include <iostream>

int fnum()
{
    int num; 
    scanf("%d",&num);
    return num;
}

void frisultato(int x, int y)
{
    if (x>y)
    {
        printf("%d",x);
        printf("%d",y);
    }
    else
    {
        printf("%d",y);
        printf("%d",x);
    }

    /* No need to return in void */
}

int main()
{    

    /*
        Variables in C are all lowercase. 

        UPPER_CASE is usually used for macros and preprocessor directives
        such as 

        #define PI 3.14 
    */
    int a, b;
    a = fnum(); //Function calls always need parenthesis, even if they are empty
    b = fnum();

    frisultato(a, b);

    /*
        Your main function should return an integer letting whoever 
        ran it know if it was successful or not. 

        0 means everything went well, anything else means something went wrong.
    */
    return 0;
}

另外,请勿在StackOverflow问题上签名。