标签: function-declaration

在函数内使用声明函数?

可能重复:
函数内部是否有函数声明?

我知道在函数内部我们可以声明一个函数.有什么用?你能带一个简单的例子吗?

c++ function function-declaration

1
推荐指数
1
解决办法
635
查看次数

R : 在函数内定义函数

(关于R语言)

我试图在另一个函数中声明/定义一个函数。它似乎不起作用。我不认为这完全是一个错误,它可能是预期的行为,但我想了解为什么!任何链接到相关手册页的答案也非常受欢迎。

谢谢

代码 :

fun1 <- function(){
  print("hello")
  fun2 <- function(){ #will hopefully define fun2 when fun1 is called
    print(" world")
  }
}

fun1() #so I expected fun2 to be defined after running this line
fun2() #aaand... turns out it isn't
Run Code Online (Sandbox Code Playgroud)

执行 :

> fun1 <- function(){
+   print("hello")
+   fun2 <- function(){ #will hopefully define fun2 when fun1 is called
+     print(" world")
+   }
+ }
> 
> fun1() #so I expected fun2 to be defined after …
Run Code Online (Sandbox Code Playgroud)

nested r function function-declaration

1
推荐指数
1
解决办法
1932
查看次数

浮点函数除法的函数声明中出现冲突类型错误

这应该是一个递归函数,当 i 小于 'h' 时,它不断向 'h' 添加 1/i 的值,但我在函数声明中不断收到冲突类型错误,我做错了什么?它只是发生在浮动中。

#include <stdio.h>

int main()
{
    int i=0, n = 5;
    float h;
    division(i, n, h);
    return 0;
}

float division(int i, int n, float h)
{
    if(i<n)
    {
        h += 1/i;
        division(i, n, h);
    }
    else
    {
        printf("the sum is: %f",h);
        return h;
    }
}
Run Code Online (Sandbox Code Playgroud)

c floating-point runtime-error division function-declaration

1
推荐指数
1
解决办法
1826
查看次数

哪些 C 规则允许在函数声明器中使用以前定义为类型的标识符?

考虑这个代码(t0.c):

typedef int T;
void f(int T);
Run Code Online (Sandbox Code Playgroud)

调用:

gcc -std=c11 -Wall -Wextra -pedantic -c t0.c
<nothing>

clang -std=c11 -Wall -Wextra -pedantic -c t0.c
<nothing>

cl /std:c11 /Za /c t0.c
t0.c(2): warning C4224: nonstandard extension used: formal parameter 'T' was previously defined as a type
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 哪些 C 规则允许在函数声明器中使用以前定义为类型的标识符?
  2. 如果 msvc 说nonstandard extension,那么为什么符合标准的 gcc / clang 什么也没说?

c language-lawyer function-declaration c11 c17

1
推荐指数
1
解决办法
60
查看次数

int foo(a) int a; 是什么意思?{ return a } 在 C 中意味着什么?

在研究函数声明时,我遇到了一种(对我来说)奇怪的函数定义方式:

int f(void); // declaration: takes no parameters
int g(); // declaration: takes unknown parameters
 
int main(void) {
    f(1); // compile-time error
    g(2); // undefined behavior
}
 
int f(void) { return 1; } // actual definition
int g(a,b,c,d) int a,b,c,d; { return 2; } // actual definition
Run Code Online (Sandbox Code Playgroud)

最后一行完全让我困惑。这是什么int g(a,b,c,d) int a,b,c,d; { return 2; }意思?这样的问题可能已经被问过,但我不知道如何组成搜索查询。

c syntax function-declaration

1
推荐指数
1
解决办法
253
查看次数

定义二维数组:int *arr[ ] 和 int arr[ ][ ] 就它们在内存中的表示方式而言有什么区别?

我一直在尝试理解这两个定义之间的区别以及如何正确使用它们。
在我看来,定义一个二维数组将arr[length][width]矩阵的内容一行一行地连续存储在内存中(矩阵本质上是一大行),这样您就可以用这种方式访问​​ i,j 坐标中的数字*(arr+width*i+j): 。当将其发送到函数时,可以进行声明,func(int *arr)并且由于二维数组在内存中的表示方式,您访问矩阵内容不会有问题。

而将数组定义为似乎int *arr[];并没有以相同的线性、连续的方式将矩阵的内容存储在内存中。函数的声明必须是func(int **arr) or (int *arr[])。虽然您仍然可以通过坐标 i, j 访问矩阵,如下所示:arr[i][j],但似乎不可能这样做:*(arr+width*i+j)。我的想法是否正确,因为以这种方式定义二维矩阵不会将矩阵的行连续存储在内存中,还是我遗漏了一些东西?

c pointers multidimensional-array implicit-conversion function-declaration

1
推荐指数
1
解决办法
466
查看次数

为什么我收到类型错误?

在Haskell中,我在定义函数时遇到了一些问题,因为我的参数类型与所需类型不匹配.

例如,我想编写一个函数,它接受n :: Int并生成从1到floor平方根的整数列表n.因此我会有一个功能,如:

list :: Int -> [Int]
Run Code Online (Sandbox Code Playgroud)

最初我定义的功能如下:

list :: Int -> [Int]

list n = [1 .. floor (sqrt n)]
Run Code Online (Sandbox Code Playgroud)

当我加载sript时,会出现类型不匹配的错误消息.但是,我不确定我是否不匹配sqrt函数或floor函数的类型.错误消息如下:

No instance for (Floating Int)
  arising from a use of 'sqrt' at pe142.hs:6:22-27
Possible fix: add an instance declaration for (Floating Int)
In the first argument of 'floor', namely '(sqrt n)'
In the expression: floor (sqrt n)
In the expression: [1 .. floor (sqrt n)]
Failed, …
Run Code Online (Sandbox Code Playgroud)

haskell compiler-errors typeerror function-declaration

0
推荐指数
1
解决办法
140
查看次数

为什么取一个被声明的函数的地址起作用?

我在这里问了一个问题,即取一个函数的地址是否强制编译所述函数,特别是关于Substitution-Failure-Is-Not-An-Error.最直接的答案可以在这里找到:

非正式地,如果一个对象的地址被占用,或者一个引用被绑定到它上面,则该对象被使用,并且如果对它进行函数调用或者取得其地址,则该函数被使用.如果一个对象或函数使用了odr,它的定义必须存在于程序的某个地方; 违反这一点的是链接时错误.

但是我测试的所有编译器都表明这是完全可行的:

void foo(int);
auto bar = &foo;
Run Code Online (Sandbox Code Playgroud)

Live Example

这不合法吗?但如果没有,为什么要建造?

c++ function-pointers addressof function-declaration function-definition

0
推荐指数
1
解决办法
103
查看次数

为什么在C中声明具有int返回类型的函数不是强制性的?

int main ()
{
hello();

return 0;
}

int hello()
{

printf("\n hello world");

return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据C规则,下面定义的每个函数main()必须在上面声明,main() 但为什么它int作为返回类型的函数的异常?如果你将返回类型hello() 改为其他任何东西(void,char*等),它将抛出错误声明.为什么int 返回类型没有错误或警告?

c function forward-declaration function-declaration

0
推荐指数
1
解决办法
102
查看次数

如何用typedef定义函数类型?

我想知道是否可以使用定义函数类型typedef,我尝试了以下语法:

typedef int (*) (void *, void *) order;
Run Code Online (Sandbox Code Playgroud)

但这是行不通的。错误信息 :expected identifier or '(' before ')' token

c typedef function-declaration

0
推荐指数
2
解决办法
67
查看次数