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? 12 5473
"Bill Pursell" <bi**********@g mail.com> writes: typedef (*FUNC)(int); FUNC f;
void * get_f(void) { return &f; }
[...]
Or am I doing something wrong?
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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
>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?
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?
According to ANSI C, you are doing something wrong.
Gordon L. Burditt
Ben Pfaff wrote: "Bill Pursell" <bi**********@g mail.com> writes:
typedef (*FUNC)(int); FUNC f;
void * get_f(void) { return &f; }
[...]
Or am I doing something wrong?
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.
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)(voi d);
void * dlopen(void) { return &f; }
int main(void)
{
FUNC fp;
fp = ((F_GETTER)dlop en)();
}
Bill Pursell wrote: Ben Pfaff wrote: "Bill Pursell" <bi**********@g mail.com> writes:
typedef (*FUNC)(int); FUNC f;
void * get_f(void) { return &f; }
[...]
Or am I doing something wrong?
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.
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)(voi d);
void * dlopen(void) { return &f; }
int main(void) { FUNC fp; fp = ((F_GETTER)dlop en)(); }
oops, I meant dlsym() in the above.
On Wed, 21 Jun 2006 12:01:08 -0700, Bill Pursell wrote: Bill Pursell wrote: Ben Pfaff wrote: > 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. 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)
<snip> oops, I meant dlsym() in the above.
dlopen() and dlsym() aren't standard C. They're Unix specific. It's a fair
bet that if they're available on your platform, then explicitly casting
between function pointers and void pointers will work as desired. But such
C code, as well as the use of those interfaces, isn't portable outside of
Unix. Here's what SUSv3 has to say about the issue:
RATIONALE
The ISO C standard does not require that pointers to functions can be
cast back and forth to pointers to data. Indeed, the ISO C standard does
not require that an object of type void * can hold a pointer to a
function. Implementations supporting the XSI extension, however, do
require that an object of type void * can hold a pointer to a function.
The result of converting a pointer to a function into a pointer to
another data type (except void *) is still undefined, however. Note that
compilers conforming to the ISO C standard are required to generate a
warning if a conversion from a void * pointer to a function pointer is
attempted as in:
fptr = (int (*)(int))dlsym( handle, "my_functio n");
Due to the problem noted here, a future version may either add a new
function to return function pointers, or the current interface may be
deprecated in favor of two new functions: one that returns data pointers
and the other that returns function pointers.
Source: http://www.opengroup.org/onlinepubs/...ons/dlsym.html
"Bill Pursell" <bi**********@g mail.com> writes: Ben Pfaff wrote: "Bill Pursell" <bi**********@g mail.com> writes:
> typedef (*FUNC)(int); > FUNC f; > > void * > get_f(void) > { > return &f; > }
[...]
> Or am I doing something wrong?
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.
This may be OT, as it's system specific, but how am I supposed to use dlopen()? It's prototyped to return a void *.
<OT>
According to the man page, dlopen() returns an opaque "handle" for the
dynamic library, not something to be used as a function pointer. It's
dlsym() that's the problem.
</OT>
An implementation may support conversion from void* to
pointer-to-function as an extension. If you have a function that
returns a void* and expects you to use it as a pointer-to-function,
that function would only be usable with an implementation that
supports such an extension. Any code that depends on such an
extension is obviously non-portable; if you invoke the compiler with
an option that asks it to warn you about non-portable code, it's going
to warn you about this.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On Wed, 21 Jun 2006 18:22:35 -0000, go***********@b urditt.org (Gordon
Burditt) wrote: 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?
There is no guarantee that a function pointer can FIT in a void * without truncating it.
If that's the case, how should I printf() the value of a function
pointer?
#include <stdio.h>
typedef void (*func_ptr_type )(void);
int main(void)
{
func_ptr_type func_ptr = NULL;
printf("val = %p\n", (void *)func_ptr);
return 0;
}
The "%p" conversion specifier requires a (void*), but if I convert a
function pointer to (void*) and that gets "truncated" , isn't there a
serious flaw in the standard?
--
jay
jaysome <ja*****@spamco p.net> writes: On Wed, 21 Jun 2006 18:22:35 -0000, go***********@b urditt.org (Gordon Burditt) wrote:
[...] There is no guarantee that a function pointer can FIT in a void * without truncating it.
If that's the case, how should I printf() the value of a function pointer?
#include <stdio.h> typedef void (*func_ptr_type )(void); int main(void) { func_ptr_type func_ptr = NULL; printf("val = %p\n", (void *)func_ptr); return 0; }
The "%p" conversion specifier requires a (void*), but if I convert a function pointer to (void*) and that gets "truncated" , isn't there a serious flaw in the standard?
There's no portable way to print the value of a function pointer,
other than by aliasing it to an array of unsigned char and printing
the bytes.
I don't think it's a serious flaw; how often do you need to print the
value of a function pointer?
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
jaysome wrote: On Wed, 21 Jun 2006 18:22:35 -0000, go***********@b urditt.org (Gordon Burditt) wrote: There is no guarantee that a function pointer can FIT in a void * without truncating it. If that's the case, how should I printf() the value of a function pointer?
Byte by byte.
#include <stdio.h> typedef void (*func_ptr_type )(void); int main(void) { func_ptr_type func_ptr = NULL; printf("val = %p\n", (void *)func_ptr); return 0; }
Try this:
#include <stdio.h>
#include <limits.h>
typedef int (*func_ptr_type )(void);
int main(void)
{
func_ptr_type func_ptr = main;
size_t i;
printf("val = 0x");
for(i = 0; i < sizeof func_ptr; i++)
{
printf("%0*X",
(CHAR_BIT + 3) / 4,
(unsigned)((uns igned char *)&func_ptr)[i]);
}
printf("\n");
return 0;
}
This compiles cleanly and prints val = 0x50104000
on my system.
The "%p" conversion specifier requires a (void*), but if I convert a function pointer to (void*) and that gets "truncated" , isn't there a serious flaw in the standard?
Not really. The %p specifier is only for printing void* pointers. It so
happens that all object pointers can be converted to unique void* values.
--
Simon. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jan-Henrik Grobe |
last post by:
Hallo,
I am coming to this newsgroup with a very strange Problem. I have two c++
files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in
B.cpp I have stored the callback functions. Important is, that the object
A.o has to be created BEFORE B can be compiled. I am sure that sounds
confusing but A.cpp is part of a library that is needed for objects I use in
B (kind of client-server thing). So I cannot include a B.h in the...
|
by: Bryan Parkoff |
last post by:
I know how to write "Pointer to Function" inside struct or class without
using static, but I have decided to add static to all functions inside
struct or class because I want member functions to be bound inside struct or
class to become global functions. It makes easier for me to use
"struct.memberfunction()" instead of "globalfunction()" when I have to use
dot between struct and member function rather than global function.
I do not have...
|
by: Thomas Barth |
last post by:
Hi,
I dont understand the third argument of this function. This function
expect the address of a function, but what does "void *(*funktion)(void
*)," mean? I am confused by so many "wildcards" :-)
int pthread_create( pthread_t *thread,
const pthread_attr_t *attribute,
void *(*funktion)(void *),
void *argumente );
|
by: joe bruin |
last post by:
hello all.
i am trying to get rid of some warnings and do "the right thing".
although in this particular case, i am not sure what the right thing
is.
the code:
typedef struct
{
|
by: Giannis Papadopoulos |
last post by:
I have the following code
#include <stdio.h>
void a(void) {
printf("a called.\n");
}
int b(void) {
printf("b called.\n");
| |
by: cmk128 |
last post by:
Hi
Why put * in front of the function name in the declaration?
int (*write)(struct inode *, struct file *, const char *, int);
thanks
from Peter (cmk128@hotmail.com)
|
by: Beta What |
last post by:
Hello,
I have a question about casting a function pointer. Say I want to make
a generic module (say some ADT implementation) that requires a function
pointer from the 'actual/other modules' that takes arguments of type
(void *) because the ADT must be able to deal with any type of data.
In my actual code, I will code the function to take arguments of their
real types, then when I pass this pointer through an interface
function, I...
|
by: Bartc |
last post by:
I'm having problems with the following code.
I have a table of function pointers, and a function pointer variable p which
steps through the functions.
But I'm not allowed to increment the variable using ++p.
What's the problem here?
Also, I may not be interested in returning from any of these functions (each
|
by: Bryan Parkoff |
last post by:
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope. The global
variables and global functions are hidden to prevent from accessing by the
programmers. All global functions share global variables.
Only very few global functions are allowed to be reusability for the
programmers to use. Few...
|
by: ghulands |
last post by:
I am having trouble implementing some function pointer stuff in c++
An object can register itself for many events
void addEventListener(CFObject *target, CFEventHandler callback,
uint8_t event);
so I declared a function pointer like
typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |