【发布时间】: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