我在这里问了一个问题,即取一个函数的地址是否强制编译所述函数,特别是关于Substitution-Failure-Is-Not-An-Error.最直接的答案可以在这里找到:
非正式地,如果一个对象的地址被占用,或者一个引用被绑定到它上面,则该对象被使用,并且如果对它进行函数调用或者取得其地址,则该函数被使用.如果一个对象或函数使用了odr,它的定义必须存在于程序的某个地方; 违反这一点的是链接时错误.
但是我测试的所有编译器都表明这是完全可行的:
void foo(int);
auto bar = &foo;
Run Code Online (Sandbox Code Playgroud)
这不合法吗?但如果没有,为什么要建造?
c++ function-pointers addressof function-declaration function-definition
我有以下cpp函数,并希望在R中编写它
我使用rcpp包编译并使用它但发生了一些错误
实际上我在R中使用指针时遇到问题
void creationPI(double *distC, int mC, int tailleC, double *PIC,int aC)
//distC: distribution de X ; mC= smax-smin+1 ie u+v+1; tailleC=a+1; PIC la matrice PI comme resultat
{
double *f;//=NULL; /*f(k)=P[X<=k]*/
int t1,k,b,c,l;
int top_un,top_deux;
//FILE *matrix;
t1=2*(aC-1)+1; // taille du vecteur des f ca va de 1-a à a-1 ; k va de [0 à 2*(a-1)]
/* ALLOCATION DES MATRICES*/
//if (!(f = (double *)calloc(t1, sizeof(double))))
//exit(ALLOC_ERROR);
f = (double *)calloc(t1, sizeof(double));
/* CREATION DES f */ …Run Code Online (Sandbox Code Playgroud) 我很高兴最近使用函数指针.我知道它们是如何工作的.函数指针的经典示例是:
int add() {
return (100+10);
}
int sub() {
return (100-10);
}
void print(int x, int y, int (*func)()) {
printf("value is : %d", (x+y+(*func)()));
}
int main() {
int x=100, y=200;
print(x,y,add);
print(x,y,sub);
}
Run Code Online (Sandbox Code Playgroud)
前几天有人问我,它比调用(内部主要)更好:
print(add(x,y));
print(sub(x,y));
Run Code Online (Sandbox Code Playgroud)
我努力解释这一点.它只是关于堆栈还是还有其他东西躺在下面?
我如何指示gcc警告我有关无效的函数指针转换,比如g ++会对下面的代码做什么?
并且..为什么gcc没有警告我这个?将指针传递给do_something()时会发生什么/将会发生什么?
#include <stdio.h>
typedef void (*void_func_t) ();
typedef void (*void_int_func_t) (int, int, int);
void do_something(void_func_t f)
{
void_int_func_t foo = f;
foo(1,2,3);
}
void a()
{
printf("a\n");
}
void b(int one, int two, int three)
{
printf("%i, %i, %i\n", one, two, three);
}
int main()
{
do_something(a);
do_something(b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
? gcc -W -Wall -Werror func.c
? ./a.out
a
1, 2, 3
Run Code Online (Sandbox Code Playgroud)
然而,c ++会警告/给出错误
g++ -W -Wall -Werror func.c
func.c: In function ‘void do_something(void_func_t)’:
func.c:8:27: error: …Run Code Online (Sandbox Code Playgroud) 我的问题是关于这两种情况:
#include <stdio.h>
int *foo1();
int *foo2();
int main()
{
printf("so it's %d\n",*foo1());
printf("so it's %d\n",*foo2());
}
int *foo1()
{
int i1 = 5;
return &i1;
}
int *foo2()
{
int i2 = 5;
int *p = NULL;
p = &i2;
return p;
}
Run Code Online (Sandbox Code Playgroud)
case1: 当它是foo1()的情况时,我们得到一个错误,因为我们试图将一个地址副本返回给main,其数据已被删除(当我们退出foo1()函数时)
case2:但是在foo2()中,虽然我们将一个副本返回到一个局部变量的指针,但是在我们退出foo2()函数后它们的数据将被删除,但它没有给出错误,为什么会这样呢?
TL; DR:为什么foo2()不会给出错误但是foo1()呢?
TIA.
我正在考虑以下两者之间的区别:
void *signal(int, void (*)(int))(int)
Run Code Online (Sandbox Code Playgroud)
和
void (*signal(int, void (*)(int)))(int)
Run Code Online (Sandbox Code Playgroud)
我知道后者来自这里 - 示例#3:"终极"(当我试着大声说出来理解它时,这是一次热闹的学习经历):
signal是一个函数(int, void (*)(int))作为输入并返回一个指向另一个获取(int)和返回的函数的指针void.
对于前者我认为,因为最后一个(int)会有更高的优先级,*所以它应该是语法错误,但是从cdecl.org,结果是:
["]声明信号为函数(int,指向函数的函数(int)返回void)返回函数(int)返回指向void [."]的指针
所以我需要一张支票.
结构box的定义box.h如下:
typedef struct box {
int (*func1) (const void *p1, const void *p2);
void (*func2) (void *p3);
} Box;
Run Code Online (Sandbox Code Playgroud)
box.c包括box.h并具有以下功能:
int create_box(int (*func1)(const void *p1, const void *p2), void (*func2)(const void *p3), Box **box) {
Run Code Online (Sandbox Code Playgroud)
create_box根据提供的参数被调用以初始化一个盒子。我正在努力弄清楚如何将提供的函数指针分配给结构的字段。首先,我首先通过分配内存给**boxvia(*box) = malloc(sizeof(Box));
但是,然后我尝试通过分配函数指针的参数
(*box)->func1 = (func1)(&p1, &p2);
(*box)->func2 = (func2)(&p3);
Run Code Online (Sandbox Code Playgroud)
运行此命令将告诉我所有指针均未声明。显然,我对函数指针的工作方式有误解,但是我不明白我应该如何初始化它们(如果这就是写词)。我试着更换右侧(*func1)(*p1, *p2),(func1)(p1, p2),(*func1)(p1, p2),(*func1)(&p1, &p2),等但他们没有得到任何东西,我能理解。如何正确初始化此结构的函数指针?
如果相关的话,我正在C90工作。
我有一些需要C函数的代码,但是我想实例化一堆类实例并将该类的成员函数作为C函数传递。我需要同时捕获N个实例。我希望我可以作为lambda来做。
这是结果函数的外观(大致):
// This is the code I want to interface with:
typedef void func(ParamClass param); // ParamClass is NOT my class
extern void notMyCode1(func funcArg); // Code I want to call, saves funcArg
extern void notMyCode2() // uses saved funcArgs
// Here is the type of class I want to instantiate:
class MyClass {
public:
MyClass(int arg) : saveArg(arg) {}
int saveArg;
void myFunc(ParamClass param) {
// uses saveArg and param to do the right action
}
};
void …Run Code Online (Sandbox Code Playgroud) 在我编写的固件中,我创建了一种变量。类似于下面的内容:
struct SitemMenu {
unsigned int id;
char * text;
void * submenu
}
typedef struct SitemMenu TitemMenu;
Run Code Online (Sandbox Code Playgroud)
是任何功能:
void functionX () {
...
}
Run Code Online (Sandbox Code Playgroud)
如果我创建此变量:
TitemMenu itemWhatever;
Run Code Online (Sandbox Code Playgroud)
并做:
itemWhatever.submenu = &function (X);
Run Code Online (Sandbox Code Playgroud)
我可以打电话给functionX做吗:
(*itemWhatever.submenu)();
Run Code Online (Sandbox Code Playgroud)
我做了类似的事情,编译器给出了这个答案:
error: (183) function or function pointer required
Run Code Online (Sandbox Code Playgroud) 为什么C中的函数声明需要知道指向函数的参数的类型和返回值?
c中的指针声明编写如下:
returnType ( *funcPtrName ) ( paramTypes )
Run Code Online (Sandbox Code Playgroud)
例如foo下面:
char my_func ( int x )
{
...
}
int main ()
{
char ( *foo ) ( int );
foo = my_func;
foo( 2 );
}
Run Code Online (Sandbox Code Playgroud)
在声明一个指向值的指针时,该类型用于确定一个元素的大小(以字节为单位)。例如int* p,中的int告诉编译器指向的元素p是sizeof( int )分开的。此信息用于指针算术。
但是,函数指针指向单个地址,并且不允许进行指针算术运算。
那么为什么需要额外的信息呢?是returnType和paramTypes仅作编译器所支持的错误检查的目的是什么?(例如,在用户分配类型不匹配的功能时提醒用户)。
c compiler-construction pointers function-pointers static-typing