功能* =无效* [英] function * = void *

查看:65
本文介绍了功能* =无效*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在使用gcc -pedantic编译时生成编译器警告



typedef(* FUNC)(int);

FUNC f;


无效*

get_f(无效)

{

返回& f;

}


int main(无效)

{

FUNC fp ;

fp =(FUNC)get_f();

返回0;

}

~


警告是:

ac:在函数`main'':

ac:14:警告:ISO C禁止转换对象指向函数的指针

指针类型


如果我删除演员,警告是:

ac:在函数`main' ':

ac:14:警告:ISO C禁止在功能指针之间进行分配

`void *''

当然我可以使用这个施工?我可以安全地忽略这个警告吗?

我宁愿在我的CFLAGS中保持-pedantic-我怎么能确保
正确地压制警告?


或者我做错了什么?

The following code generates a compiler warning
when compiled with gcc -pedantic:

typedef (*FUNC)(int);
FUNC f;

void *
get_f(void)
{
return &f;
}

int main(void)
{
FUNC fp;
fp = (FUNC)get_f();
return 0;
}
~

The warning is:
a.c: In function `main'':
a.c:14: warning: ISO C forbids conversion of object pointer to function
pointer type

If I remove the cast, the warning is:
a.c: In function `main'':
a.c:14: warning: ISO C forbids assignment between function pointer and
`void *''
Surely I can use this construction? Can I safely ignore the warning?
I''d prefer to keep -pedantic in my CFLAGS---how can I
correctly suppress the warning?

Or am I doing something wrong?

推荐答案

" Bill Pursell" <双********** @ gmail.com>写道:
"Bill Pursell" <bi**********@gmail.com> writes:
typedef(* FUNC)(int);
FUNC f;

void *
get_f(void)
{
返回& f;
}


[...]

或者我做错了什么?
typedef (*FUNC)(int);
FUNC f;

void *
get_f(void)
{
return &f;
}
[...]
Or am I doing something wrong?




是的。该标准不允许在函数和

对象指针之间进行转换。为什么不改变get_f()来返回一个函数

指针?它不一定是*正确*类型的函数

指针:从一种函数指针转换为另一种函数指针

允许且明确定义,如只要你没有通过不兼容的函数指针实际调用

函数。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1 ; putchar(p [i] \

);} return 0;}



Yes. The standard doesn''t allow conversion between function and
object pointers. Why not change get_f() to return a function
pointer? It doesn''t have to be the *correct* kind of function
pointer: casts from one kind of function pointer to another are
allowed and well-defined, as long as you don''t actually call a
function through an incompatible function pointer.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


>以下代码生成编译器警告
>The following code generates a compiler warning
使用gcc -pedantic编译时:

typedef(* FUNC)(int);
FUNC f;

void *
get_f(void)
返回& f;
}
int main(无效)
{/ f> fUNC fp;
fp =(FUNC)get_f();
返回0; <警告是:
ac:在函数`main'':
ac:14:警告:ISO C禁止转换对象指向函数的指针
指针类型

如果我删除演员,警告是:
ac:在函数`main'':
ac:14:警告: ISO C禁止在功能指针和
之间进行分配。

当然我可以使用这种结构吗?


无法保证函数指针可以在void *

中进行FIT而不截断它。在Intel x86平台上,有中等的
和紧凑模型,其中函数指针的大小与数据指针的大小不同(在一个数据指针上)更大,

另一方面功能指针更大)。


如果有新的平台可以让我感到惊讶/>
是64位(或者可能是48位)的函数和数据指针,以及32位的
。问题不仅仅是古老的硬件。


你*可以*使用构造fflush(主)如果你甚至可以找到一个

平台它的工作原理上。我不知道你会因此而被捕的任何国家。我不明白为什么这是一个问题。

我能安全地忽略这个警告吗?


这取决于您希望代码的可移植性,以及您实际运行的
平台。你正在编写代码

这使得无效的假设和问题不太可能只是古老的硬件。

我会我喜欢在我的CFLAGS中保持-dantantic - 我怎么能正确地压制警告呢?

或者我做错了什么?
when compiled with gcc -pedantic:

typedef (*FUNC)(int);
FUNC f;

void *
get_f(void)
{
return &f;
}

int main(void)
{
FUNC fp;
fp = (FUNC)get_f();
return 0;
}
~

The warning is:
a.c: In function `main'':
a.c:14: warning: ISO C forbids conversion of object pointer to function
pointer type

If I remove the cast, the warning is:
a.c: In function `main'':
a.c:14: warning: ISO C forbids assignment between function pointer and
`void *''
Surely I can use this construction?
There is no guarantee that a function pointer can FIT in a void *
without truncating it. On Intel x86 platforms, there are medium
and compact models where the size of a function pointer is not the
same as that of a data pointer (on one the data pointer is larger,
on the other the function pointer is larger).

It wouldn''t surprise me if there will be new platforms where there
are function and data pointers of 64-bits (or perhaps 48 bits) and
32-bits. The issue isn''t just ancient hardware.

You *can* use the construct fflush(main) if you can find even one
platform it works on. I don''t know of any country where you would
be arrested for that. I don''t understand why this is an issue.
Can I safely ignore the warning?
It depends on how portable you want your code to be, and what
platform you are actually running on. You are writing code
which makes invalid assumptions and the problem isn''t likely
to just be ancient hardware.
I''d prefer to keep -pedantic in my CFLAGS---how can I
correctly suppress the warning?

Or am I doing something wrong?




根据ANSI C,你做错了。


Gordon L. Burditt



According to ANSI C, you are doing something wrong.

Gordon L. Burditt




Ben Pfaff写道:

Ben Pfaff wrote:
" Bill Pursell" <双********** @ gmail.com>写道:
"Bill Pursell" <bi**********@gmail.com> writes:
typedef(* FUNC)(int);
FUNC f;

void *
get_f(void)
{
返回& f;
}
typedef (*FUNC)(int);
FUNC f;

void *
get_f(void)
{
return &f;
}



[...]



[...]

或者我在做什么有什么不对吗?
Or am I doing something wrong?



是的。该标准不允许在函数和对象指针之间进行转换。为什么不改变get_f()来返回一个函数指针?它不一定是*正确*类型的函数
指针:从一种函数指针到另一种函数指针的转换允许和明确定义,只要你实际上没有通过不兼容的函数指针调用
函数。



Yes. The standard doesn''t allow conversion between function and
object pointers. Why not change get_f() to return a function
pointer? It doesn''t have to be the *correct* kind of function
pointer: casts from one kind of function pointer to another are
allowed and well-defined, as long as you don''t actually call a
function through an incompatible function pointer.




这可能是OT,因为它是特定于系统的,但是我怎么认为

使用dlopen()?它的原型是返回空白*。我应该这样做:/ b $ b类似于:(无法编译)


typedef int(* FUNC)(int);

FUNC f;


typedef FUNC *(F_GETTER)(无效);


void * dlopen(void){return& f; }


int main(无效)

{

FUNC fp;

fp =(( F_GETTER)dlopen)();

}



This may be OT, as it''s system specific, but how am I supposed
to use dlopen()? It''s prototyped to return a void *. Should I do
something like: (doesn''t compile)

typedef int (*FUNC)(int);
FUNC f;

typedef FUNC *(F_GETTER)(void);

void * dlopen(void) { return &f; }

int main(void)
{
FUNC fp;
fp = ((F_GETTER)dlopen)();
}


这篇关于功能* =无效*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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