我正在尝试构建一个 CARDIAC C
中的计算机 模拟器,我到了需要从函数内部调用函数的阶段。
在下面的代码中,我试图调用函数
JAZ()
,在函数 CARDIAC()
中,我得到一个明显的错误,说我不能调用它因为它不是预先定义的。
我需要做什么才能让编译后的程序
printf
“Point reached!”
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int input;
void CARDIAC(int* ptr)
{
int input = *ptr;
while(input<900)
{
if(input<100 && input>=0)
{
// INP();
}
else if(input<200 && input>99)
{
printf("Point reached!\n");
break;
}
else if(input<300 && input>199)
{
// LDA();
}
else if(input<400 && input>299)
{
// LDI();
}
else if(input<500 && input>399)
{
// STA();
}
else if(input<600 && input>499)
{
// STI();
}
else if(input<700 && input>599)
{
// ADD();
}
else if(input<800 && input>699)
{
// SUB();
}
else if(input<900 && input>799)
{
JAZ();
}
else
{
// HRS();
}
}
printf("Done\n");
}
void JAZ()
{
input = 180;
CARDIAC(&input);
}
int main()
{
input=820;
CARDIAC(&input);
return 0;
}
这是我得到的输出:
test.c: In function ‘CARDIAC’:
test.c:47:4: warning: implicit declaration of function ‘JAZ’ [-Wimplicit-function-declaration]
JAZ();
^~~
test.c: At top level:
test.c:57:6: warning: conflicting types for ‘JAZ’
void JAZ()
^~~
test.c:47:4: note: previous implicit declaration of ‘JAZ’ was here
JAZ();
^~~
最佳答案
你必须把函数原型(prototype)放在两个函数之前,比如:
#include <stdio.h>
#include <stdlib.h>
int input;
/* function prototypes */
void CARDIAC(int *ptr);
void JAZ(void);
void CARDIAC(int* ptr)
{
int input = *ptr;
....
关于从函数内部调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622950/