解除引用函数指针类型 [英] dereferencing function pointer types

查看:67
本文介绍了解除引用函数指针类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有program1.c:


typedef int(* fn_t)(int);


int fn(int f){

返回f;

}


int main(void) {$ / $

fn_t f = fn;


返回f(0);

}


和program2.c

typedef int(* fn_t)(int);


int fn(int f ){

返回f;

}


int main(无效){


fn_t f = fn;


返回(* f)(0);

}


他们

gcc -Wall -ansi -pedantic

并且实际上生成相同的可执行文件,并且没有错误/警告/评论。


函数指针的故事是什么?

取消引用他们打电话给他们是不错的风格? Gcc甚至不会抱怨返回

(** f)(0); !


我会猜到,因为我可以写f = fn; f和fn有相同类型的

,但Gcc并没有抱怨f =& FN;或者,当你看到typedef时,

更有意义。


所以我应该写f = fn或f =& fn,我应该写f(0);或(* f)(0);


如果函数类似于

数组类型,后者在每种情况下都会执行前者如果指针就像标量一样有道理。


当然标准不能允许两者 - 这看起来很邋。


谢谢,

viza

解决方案

7月1日晚上9:11,viza< tom.v ... @ gmil。编写:





我有program1.c:


typedef int(* fn_t)(int);


int fn(int f){

返回f;


}


int main(无效){


fn_t f = fn;


返回f(0);


}


和program2.c


typedef int(* fn_t)(int);


int fn(int f){

返回f;


}


int main(无效){


fn_t f = fn;


返回(* f )(0);


}


他们都编译没有错误/警告/评论

gcc -Wall -ansi - 迂腐


实际上产生相同的可执行文件。


函数指针的故事是什么?

取消引用他们打电话给他们是不错的风格? Gcc甚至不会抱怨返回

(** f)(0); !



取消引用函数指针什么都不做。


我会猜到,因为我可以写f = fn; f和fn有相同类型的

,但Gcc并没有抱怨f =& FN;或者,当你看到typedef时,

更有意义。


所以我应该写f = fn或f =& fn,我应该写f(0);或(* f)(0);



我更喜欢f = fn。至于后者,一些程序员/项目更喜欢使用(* f)(0)来清除''f''是一个函数指针;我更喜欢f(0)



前者在每种情况下都是如果函数就像

数组类型,如果指针类似于标量,则后者是有意义的。



函数指针是标量。但*和&的效果没有

适用于他们。


当然,标准不能允许两者 - 这似乎相当草率。



确实


vi ****** @ gmail.com 写道:


7月1日,9:11 pm,viza < tom.v ... @ gmil.comwrote:


>嗨

我有program1.c:

typedef int(* fn_t)(int);

int fn(int f){
返回f;

}

int main(void){

fn_t f = fn;

返回f(0);

}

和program2.c

typedef int(* fn_t)(int);

int fn(int f){
return f;

}
int main(无效){

fn_t f = fn;

return(* f)(0);

}

他们都在
gcc -Wall -ansi -pedantic
下编译没有错误/警告/评论,事实上生成相同的可执行文件。

函数指针的故事是什么?将它们称为取消引用它们是一种好的风格吗? Gcc甚至没有抱怨回归
(** f)(0); !



取消引用函数指针什么都不做。


>我猜对了,因为我可以写f = fn; f和fn有相同的类型,但Gcc并没有抱怨f =& FN;或者,当你看到typedef时更有意义。

我应该写f = fn或f =& fn,我应该写f(0);或(* f)(0);



我更喜欢f = fn。至于后者,一些程序员/项目更喜欢使用(* f)(0)来清除''f''是一个函数指针;我更喜欢f(0)



>前者在每种情况下都是如果函数就像一个
数组类型,如果指针像标量,后者是有意义的。



函数指针是标量。但*和&的效果没有

适用于他们。



这非常接近正确。

但是如果它是正确的那么,

那么( && f)(0)与(& f)(0),

的意思相同,并且它不会。

(& & f)(0)未定义。

函数类型的表达式转换为指针类型的

表达式,除了两个上下文之外:

1& (函数类型表达式)

2 sizeof(函数类型表达式)


由于sizeof没有为函数类型的表达式定义,

你能做的唯一事情

,在正确的C程序中使用函数类型

的表达式,是从它派生一个指针。


函数调用描述了一个指向函数类型表达式的指针

后跟函数调用操作符,

这是函数调用中的括号。


因此,如果func是函数调用中函数的名称



它将被转换为指针。


func();



(& func)();
<相同br />


(* func)();

函数的名称隐式转换为指针;

,间接运算符产生一个函数类型

的表达式,该表达式转换为指针。

所以,

(**** ***************************** func)();

也和
func();

但是

(& func)();

你有结果地址运算符

来自函数类型的操作数。

这是一个函数指针,如果你应用&为此,

你得到地址运营商的结果地址

,这意味着什么。


-

pete


7月1日晚上11点28分,pete< pfil ... @ mindspring.comwrote:


vipps ... @ gmail.com写道:


7月1日晚9点11分,viza< tom。 v ... @ gmil.comwrote:


如果函数类似于

数组类型,那么前者在每种情况下都是这样做的如果指针就像标量那么后者就有意义了。



函数指针是标量。但*和&的效果没有

适用于他们。



那是非常接近正确的。

但是如果它是正确的那么,

那么( && f)(0)与(& f)(0),

的意思相同,并且它不会。

(& & f)(0)未定义。



不,我是对的。 (&& f)(0)是语法错误。 &安培;&安培;是合乎逻辑的

运算符,它与我声称'&''
的效果不适用于函数指针无关。


Hi

I have program1.c:

typedef int (*fn_t)(int);

int fn( int f ){
return f;
}

int main( void ){

fn_t f= fn;

return f( 0 );
}

and program2.c

typedef int (*fn_t)(int);

int fn( int f ){
return f;
}

int main( void ){

fn_t f= fn;

return (*f)( 0 );
}

They both compile with no errors/warnings/comments under
gcc -Wall -ansi -pedantic

and in fact produce identical executables.

What is the story with function pointers? Is it good style to
dereference them to call them? Gcc doesn''t even complain about return
(**f)(0); !

I would have guessed that since I can write f= fn; that f and fn have the
same type, but then Gcc doesn''t complain about f= & fn; either, which
makes more sense when you look at the typedef.

So should I write f= fn or f= &fn, and should I write f(0); or (*f)(0);

The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.

Surely the standard can''t permit both - that seems quite sloppy.

Thanks,
viza

解决方案

On Jul 1, 9:11 pm, viza <tom.v...@gmil.comwrote:

Hi

I have program1.c:

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return f( 0 );

}

and program2.c

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return (*f)( 0 );

}

They both compile with no errors/warnings/comments under
gcc -Wall -ansi -pedantic

and in fact produce identical executables.

What is the story with function pointers? Is it good style to
dereference them to call them? Gcc doesn''t even complain about return
(**f)(0); !

Dereferencing a function pointer does nothing.

I would have guessed that since I can write f= fn; that f and fn have the
same type, but then Gcc doesn''t complain about f= & fn; either, which
makes more sense when you look at the typedef.

So should I write f= fn or f= &fn, and should I write f(0); or (*f)(0);

I prefer f = fn. As for the latter, some programmers/projects prefer
to have (*f)(0) to make clear ''f'' is a function pointer; I prefer f(0)
though.

The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.

Function pointers are scalars. But the effect of * and & does not
apply to them.

Surely the standard can''t permit both - that seems quite sloppy.

It does


vi******@gmail.com wrote:

On Jul 1, 9:11 pm, viza <tom.v...@gmil.comwrote:

>Hi

I have program1.c:

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return f( 0 );

}

and program2.c

typedef int (*fn_t)(int);

int fn( int f ){
return f;

}

int main( void ){

fn_t f= fn;

return (*f)( 0 );

}

They both compile with no errors/warnings/comments under
gcc -Wall -ansi -pedantic

and in fact produce identical executables.

What is the story with function pointers? Is it good style to
dereference them to call them? Gcc doesn''t even complain about return
(**f)(0); !

Dereferencing a function pointer does nothing.

>I would have guessed that since I can write f= fn; that f and fn have the
same type, but then Gcc doesn''t complain about f= & fn; either, which
makes more sense when you look at the typedef.

So should I write f= fn or f= &fn, and should I write f(0); or (*f)(0);

I prefer f = fn. As for the latter, some programmers/projects prefer
to have (*f)(0) to make clear ''f'' is a function pointer; I prefer f(0)
though.

>The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.

Function pointers are scalars. But the effect of * and & does not
apply to them.

That''s very close to being correct.
But if it were correct,
then (&&f)(0) would mean the same thing as (&f)(0),
and it doesn''t.
(&&f)(0) is undefined.
An expression of function type is converted to an
expression of pointer type, in all but two contexts:
1 & (function type expression)
2 sizeof(function type expression)

Since sizeof is not defined for expressions of function type,
the only thing that you can do
with an expression of function type
in a correct C program, is to derive a pointer from it.

A function call is described a pointer to a function type expression
followed by the function call operator,
which is the parentheses in a function call.

So, if func is the name of a function
in a function call,
it is converted to a pointer.

func();
does the same thing as
(&func)();

In
(*func)();
the name of the function is converted to a pointer implicitly;
the indirection operator yields an expression of function type
and that expression is converted to a pointer.
So,
(*********************************func)();
also does the same thing as
func();
But with
(&func)();
you have the result of the address operator
from a function type operand.
That''s a function pointer, and if you apply & to that,
you get the address of the result of the address operator
and that means nothing.

--
pete


On Jul 1, 11:28 pm, pete <pfil...@mindspring.comwrote:

vipps...@gmail.com wrote:

On Jul 1, 9:11 pm, viza <tom.v...@gmil.comwrote:

The former in each case is what you would do if functions were like an
array type, and the latter makes sense if pointers were like scalars.

Function pointers are scalars. But the effect of * and & does not
apply to them.


That''s very close to being correct.
But if it were correct,
then (&&f)(0) would mean the same thing as (&f)(0),
and it doesn''t.
(&&f)(0) is undefined.

No, I''m correct. (&&f)(0) is a syntax error. && is the logical
operator, it has nothing to do with my claim that the effect of ''&''
does not apply to function pointers.


这篇关于解除引用函数指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆