【问题标题】:Calling a function in from main()从 main() 调用函数
【发布时间】:2019-10-07 14:02:02
【问题描述】:

我刚刚开始学习 C,现在我发现如何从另一个函数调用一个函数有点令人困惑。这是我的小项目,我认为扭动“int result = timeToWork;”这一行调用该函数就足够了,但是会出现警告“从 'int(*)(int)' 初始化 'int' 使指针中的整数没有强制转换”。该项目编译,但结果是一些奇怪的数字而不是打印该行。我做错了什么?

#include<stdio.h>
int timeToWork(int input);
int main()
{
    printf("Please, enter the number  \n");
    int key = 0;
    fflush(stdout);
    scanf("%d", &key);
    int result = timeToWork;
    printf("Time = %d  \n",timeToWork);

    return 0;
}
int timeToWork(int input)
{
    if(input == 1)printf("It will take you 25 minutes to get to your destination by car  \n");
    else if(input == 2)printf("It will take you 20 minutes to get to your destination by bike  \n");
    else if(input == 3)printf("It will take you 35 minutes to get to your destination by bus  \n");
    else if(input == 4)printf("It will take you 30 minutes to get to your destination by train  \n");
    else printf("ERROR: please, enter a number from 1 to 4  \n");

    return 0;
}

【问题讨论】:

  • “我刚刚开始学习 C” - 您使用什么资源进行学习?这些资源是否涵盖了如何调用函数?如果是,请说明他们所说的内容以及您不清楚的原因。如果没有,也许您应该关注该资源,直到它完成为止。对于这些非常基本的事情,从培训资源中学习可能比尝试从 Stack Overflow 拼凑你的答案更好。
  • 无论你从哪个来源学习 C,都应该有一个关于调用函数的部分 - 为什么不参考那个?
  • 您可能想要实际调用该函数:int result = timeToWork(key).
  • 您应该像初学者的 C 教科书或您学习 C 的任何资源中的示例一样格式化您的代码。这很重要。
  • 顺便说一句:警告 "initialization of 'int' from 'int(*)(int)' make integer from pointer without a cast" 几乎总是一个错误。大多数其他警告(如果不是全部)应视为错误。

标签: c function definition function-call


【解决方案1】:

函数只有一个参数。

int timeToWork(int input);

那么在不向函数传递参数的情况下如何调用它呢?

int result = timeToWork;

即使函数不接受参数,您也必须在函数 deisgnator 之后指定括号。

在上面的声明中,函数指示符只是转换为指向函数的指针

看起来像

int( *fp )( int ) = timeToWork;
int result = fp;

要调用你应该编写的函数

int result = timeToWork( key );

而不是

printf("Time = %d  \n",timeToWork);

你的意思很明显

printf("Time = %d  \n", result);

虽然函数定义不正确,因为它总是返回 0。

我认为函数应该按以下方式声明和定义

int timeToWork( int input )
{
    int time = 0;

    if(input == 1)
    {
        printf("It will take you 25 minutes to get to your destination by car  \n");
        time = 25;
    }
    else if(input == 2)
    {
        printf("It will take you 20 minutes to get to your destination by bike\n");
        time = 20;
    }
    else if(input == 3)
    {
        printf("It will take you 35 minutes to get to your destination by bus  \n");
        time = 35;
    }
    else if(input == 4)
    {
        printf("It will take you 30 minutes to get to your destination by train  \n");
        time = 30;
    }
    else
    {
         printf("ERROR: please, enter a number from 1 to 4  \n");
    }

    return time;
}

【讨论】:

  • 第一句话听起来有点像“如果timeToWork() 没有参数,int result = timeToWork; 就可以了”。也许你想澄清一下
  • time = input*5+20、It will take you %d minutes 和 if((unsigned)input-1&lt;4) { /* do stuff */ } 怎么样?
【解决方案2】:

您的函数 timeToWork 需要输入。

应该是int result = timeTowork(key);

另外,这里要打印什么?: printf("Time = %d \n",timeToWork);

【讨论】:

    【解决方案3】:

    您需要发送 int 类型的参数,因为函数需要它。

    int result = timetowork(#your input goes here);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2013-05-28
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2016-06-13
      相关资源
      最近更新 更多