473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

function * = void *

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?

Jun 21 '06 #1
12 5416
"Bill Pursell" <bi**********@gmail.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[]="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;}
Jun 21 '06 #2
>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
Jun 21 '06 #3

Ben Pfaff wrote:
"Bill Pursell" <bi**********@gmail.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)(void);

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

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

Jun 21 '06 #4

Bill Pursell wrote:
Ben Pfaff wrote:
"Bill Pursell" <bi**********@gmail.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)(void);

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

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


oops, I meant dlsym() in the above.

Jun 21 '06 #5
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_function");

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

Jun 21 '06 #6
"Bill Pursell" <bi**********@gmail.com> writes:
Ben Pfaff wrote:
"Bill Pursell" <bi**********@gmail.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_Keith) 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.
Jun 21 '06 #7
On Wed, 21 Jun 2006 18:22:35 -0000, go***********@burditt.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
Jun 22 '06 #8
jaysome <ja*****@spamcop.net> writes:
On Wed, 21 Jun 2006 18:22:35 -0000, go***********@burditt.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_Keith) 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.
Jun 22 '06 #9
jaysome wrote:
On Wed, 21 Jun 2006 18:22:35 -0000, go***********@burditt.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)((unsigned 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.
Jun 22 '06 #10
On Thu, 22 Jun 2006 07:42:20 GMT, Simon Biber <ne**@ralmin.cc> wrote:
jaysome wrote:
On Wed, 21 Jun 2006 18:22:35 -0000, go***********@burditt.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)((unsigned char *)&func_ptr)[i]);
}
printf("\n");
return 0;
}

This compiles cleanly and prints val = 0x50104000
on my system.


It prints val = 0x00104000 on my system, which has a 64-bit dual-core
CPU. I'll try running the program on our DEC Alpha tomorrow and report
back the results.
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.


So in other words, you can printf() the value of an object pointer,
but you can't necessarily printf() the value of a a function pointer.

Someday, I'll read the standard to see if what you're saying is true.
In the meantime, I'm gonna perform a test on every platform/compiler I
work with. I think I'll be busy for a couple of weeks, if not a month.

--
jay

Jun 22 '06 #11
jaysome <ja*****@spamcop.net> writes:
On Thu, 22 Jun 2006 07:42:20 GMT, Simon Biber <ne**@ralmin.cc> wrote: [...]
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.


So in other words, you can printf() the value of an object pointer,
but you can't necessarily printf() the value of a a function pointer.


Right.
Someday, I'll read the standard to see if what you're saying is true.
Today would be a good day.
In the meantime, I'm gonna perform a test on every platform/compiler I
work with. I think I'll be busy for a couple of weeks, if not a month.


Sure, have fun. It's entirely possible that every platform you try
will allow function pointers to be converted to void*, but that won't
really tell you anything about the standard.

--
Keith Thompson (The_Other_Keith) 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.
Jun 22 '06 #12
On Thu, 22 Jun 2006 01:08:43 -0700, jaysome <ja*****@spamcop.net>
wrote:

Someday, I'll read the standard to see if what you're saying is true.
In the meantime, I'm gonna perform a test on every platform/compiler I
work with. I think I'll be busy for a couple of weeks, if not a month.


It would be quicker to read the standard.

--
Al Balmer
Sun City, AZ
Jun 22 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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...
1
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...
3
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" :-)...
3
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 {
5
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");
9
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)
3
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'...
9
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...
12
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....
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.