473,943 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
12 5493
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***********@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.


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*****@spamco p.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_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.
Jun 22 '06 #12
On Thu, 22 Jun 2006 01:08:43 -0700, jaysome <ja*****@spamco p.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
425
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...
1
3990
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...
3
2334
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 );
3
3000
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
2179
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
5106
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
3670
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...
9
9033
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
12
3043
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...
7
3837
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);
0
9970
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11133
jinu1996
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...
1
11304
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,...
0
10666
tracyyun
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...
0
9866
agi2029
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...
1
8232
isladogs
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...
0
6090
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...
0
6312
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3516
bsmnconsultancy
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...

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.