我正在制作一个与此类似的代码:
#include <stdio.h>
double some_function( double x, double y)
{
double inner_function(double x)
{
// some code
return x*x;
}
double z;
z = inner_function(x);
return z+y;
}
int main(void)
{
printf("%f\n", some_function(2.0, 4.0));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在GCC中完美编译(没有警告)但无法在ICC中编译.
ICC给出:
main.c(16): error: expected a ";"
{
^
main.c(21): warning #12: parsing restarts here after previous syntax error
double z;
^
main.c(22): error: identifier "z" is undefined
z = inner_function(x);
^
compilation aborted for main.c (code 2)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢.
(编辑)抱歉这个可怜的例子.在我的原始代码中,我有点需要做这些事情.我正在使用GSL数值积分器,有类似的东西:
double stuff(double …Run Code Online (Sandbox Code Playgroud) 在装配中实现它似乎不太难.
gcc还有一个标志(-fnested-functions)来启用它们.
如果C或C++中有可能,有人可以告诉我吗?
void fun_a();
//int fun_b();
...
main(){
...
fun_a();
...
int fun_b(){
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
或类似的东西,例如函数内的类?
谢谢你的回复,
请参阅以下代码:
/* first file */
int i; /* definition */
int main () {
void f_in_other_place (void); /* declaration */
i = 0
return 0;
}
/* end of first file */
/* start of second file */
extern int i; /* declaration */
void f_in_other_place (void){ /* definition */
i++;
}
/* end of second file */
Run Code Online (Sandbox Code Playgroud)
我知道外部对象有external链接,内部对象有none链接(extern暂时忽略)。现在,如果我谈论函数f_in_other_place(),它是在 main 函数中声明的。那么它的标识符会被视为内部对象吗?如果是,则它应该具有none链接,但在程序中可见,此函数指的是它在第二个文件中的定义,该文件显示它的标识符的行为类似于具有external链接的对象。所以我很困惑这里的这个标识符是否有external链接或none链接?
现在谈到extern关键字,我在某处读到了函数声明隐式前缀 …
今天我遇到了我从未听说过的嵌套功能.它只是GNU C的一部分吗?
这是一个嵌套函数的维基百科示例.
float E(float x)
{
float F(float y)
{
return x + y;
}
return F(3);
}
Run Code Online (Sandbox Code Playgroud)
从代码中看,嵌套函数看起来像C++内联函数.那么,是否有可能取出嵌套函数的地址?
编辑:
Adam给出的gcc链接说嵌套函数的代码是在堆栈上动态创建的.但是如何从堆栈运行代码?不应该在代码段中.
我在网上考试中发现了这个问题.这是代码:
#include <stdio.h>
int main(void) {
int demo();
demo();
(*demo)();
return 0;
}
int demo(){
printf("Morning");
}
Run Code Online (Sandbox Code Playgroud)
我在测试后看到了答案.这就是答案:
MorningMorning
Run Code Online (Sandbox Code Playgroud)
我看了解释,但不明白为什么这就是答案.我的意思是,这条线不应该int demo();引起任何"问题"吗?
任何解释都有帮助.谢谢.
function1()
{
statement1;
statement2;
function2()
{
statement3;
statement3;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么控制不进入function2,即使两个函数的返回类型相同
我最近一直在编写像Lua这样的脚本语言,并且匿名内部函数的存在让我思考.用C语言实现的语言(如Lua)如何在C语言中具有内部函数,无论您做什么,都无法避免在编译期间必须事先声明函数的事实?这是否意味着在C中,实际上有一种方法可以实现内部函数,而这仅仅是实现庞大的代码库以使它们成为可能的问题?
例如
void *block = malloc(sizeof(1) * 1024); // somehow
// write bytes to this memory address to make it operate
// like an inner function?
// is that even possible?
char (*letterFunct)(int) = ((char (*letterFunct)(int))block;
// somehow trick C into thinking this block is a function?
printf("%c\n", (*letterFunct)(5)); // call it
Run Code Online (Sandbox Code Playgroud)
我缺少的关键概念是如何弥合这一差距,理解为什么某些具有高级功能的语言(类,对象,内部函数,多线程)可以用没有所有这些语言的语言实现?
以下链接提到C中不存在嵌套函数
在以下文件函数中,mini_vsnprintf具有嵌套函数
https://github.com/mludvig/mini-printf/blob/master/mini-printf.c
问:嵌套函数真的存在于C中吗?
#include <stdio.h>
int tempconvert(int, int, int);
int main(void) {
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
int tempconvert(lower, upper, step)
{
fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里有关于C编程书示例的新增内容.试图运行此代码,并编译,但printf拒绝实际打印任何东西.我可以使用这本书的例子继续前进,但我不知道为什么我的代码不能正常工作.
对这里我可能缺少的任何帮助或见解都会很精彩.