473,883 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing the address of a function


i am trying to print the address of a function without getting a
compiler warning (i am compiling with gcc with alot of flags).

if i try this:

printf("%p", f);

i get:

warning: format %p expects type 'void *; but argument 2 has type 'void
(*) void)'

if i try this:

printf("%p", (void *)f);

i get:

ISO C forbids conversion of function pointer to object pointer type

if i try this:

printf("%x", ((unsigned int)f));

it compiles cleanly, but this solution bugs me because it assumes that
an unsigned int is the same length as a pointer.

any suggestions as to the "correct" way to print the address of a function?

thanks,
rCs

Sep 25 '06
57 5698
Walter Roberson wrote:
In article <11************ **********@e3g2 000cwe.googlegr oups.com>,
Robert Gamble <rg*******@gmai l.comwrote:
I believe the following program is conforming:
void print_func_addr (void (*fp)()) {
unsigned char *cp = (unsigned char *)&fp;
[...]
If you examine the code snippet above, you will note that you
are converting a pointer to a function into a pointer to an object.
No, a pointer to a pointer to a function is not a pointer to a
function. (Note the & before fp.)

Sep 26 '06 #21
Walter Roberson wrote:
In article <11************ **********@e3g2 000cwe.googlegr oups.com>,
Robert Gamble <rg*******@gmai l.comwrote:
I believe the following program is conforming:
void print_func_addr (void (*fp)()) {
unsigned char *cp = (unsigned char *)&fp;

According to C89 (3.2.2.3 Pointers)

A pointer to void may be converted to or from a pointer to any
incomplete or object type. A pointer to any incomplete or object
type may be converted to a pointer to void and back again;
the result shall compare equal to the original pointer.

In another location, void is defined as an incomplete type that
cannot be completed.

In the section on cast operators (C89 3.3.4):

A pointer to an object or incomplete type may be converted to a
pointer to a different object type or a different incomplete type.
The resulting pointer might not be valid if it is improperly aligned
for the type pointed to. [...]

A pointer to a function of one type may be converted to a pointer
to a function of anotehr type and back again; the result shall compare
equal to the original pointer. [...]
If you examine the code snippet above, you will note that you
are converting a pointer to a function into a pointer to an object.
No, I am converting a "pointer *to a pointer* to a function" to a
"pointer to unsigned char".
Although the standard guarantees that pointers to void will have
the same representation and alignment restrictions as pointers to
char, you aren't using pointers to void -- and the standard
does not guarantee that a pointer to a function may be
meaningfully cast to a pointer to anything else.
But it does guarantee that a pointer to a function type can be
converted to a pointer to any other function type and back again which
implies that the size of all function pointers are the same. This
doesn't neccessarily mean that all pointers to functions have the same
representation and if they don't then it might be required to print the
representation of the function pointer without converting it to a
different function type to achieve the desired result, YMMV.
Even converting a pointer to a function into a pointer to void is not amongst the
defined operations.
Correct.

Robert Gamble

Sep 26 '06 #22
Harald van Dijk wrote:
Robert Seacord wrote:
i am trying to print the address of a function

This cannot be done portably. ...you may be able to store
the address in a variable, and access that variable as an array of
unsigned char, printing each element.

void (*fp)(void);
/* ... */
for (unsigned char *c = (unsigned char *) &fp; c != (unsigned char *)
(&fp + 1); c++)
printf("%x ", (unsigned) *c);
You can avoid the space(s) in the output by rounding CHAR_BIT to a
number of nibbles and printing a fixed number of hex digits for each
byte.

#define CHAR_NIBBLES ((CHAR_BIT + 3)/4)

printf("%0*x", CHAR_NIBBLES, 0u + *c);

--
Peter

Sep 26 '06 #23
On Mon, 25 Sep 2006 20:53:51 GMT, Frederick Gotham
<fg*******@SPAM .comwrote in comp.lang.c:
Robert Seacord posted:
ISO C forbids conversion of function pointer to object pointer type
Actually, the warning is incorrect. The C standard does not forbid
this. It produces undefined behavior by the lack of a definition. The
C standard specifically defines converting a pointer to any object
type to and from pointer to void, and certain other conversions
between pointers to object types with an appropriate cast.

It just plain does not define any conversions for pointers to
functions, hence any attempt to do so is undefined.
This is the first I have heard of this restriction. The smallest addressable
unit of memory in C is the byte (sidestepping the technicalities of bitfields
and so forth...). Four separate built-in pointer types are guaranteed to
store accurately the address of a byte:

char*
char signed*
char unsigned*
void*

(and all their const/volatile/restrict variants.)
Nowhere in the standard does it say that they can address every byte
that exists in a machine, merely every byte that an executable can
access.
On these grounds, I don't understand the logic pertaining to why we can't
simply do:

void *p = (void*)SomeFunc ;
You are displaying your ignorance of the depth and breadth of systems
on which C is implemented. You are assuming many things:

1. A pointer to function is an address in the same way that a pointer
to an object type is. There are some systems where this is indeed not
true.

2. That if a pointer to function is indeed a memory address, that it
is in the same memory space as data occupied by objects. Again, there
are platforms where this is not true.
Sure, the Standard forbids it... but do the laws of physics not guarantee its
success... ?
No, the standard does not forbid it. And the laws of physics have
nothing at all to do with. But on some platforms either the OS or the
hardware architecture prevent if from succeeding in any meaningful
way.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 26 '06 #24
Peter Nilsson wrote:
Harald van Dijk wrote:
Robert Seacord wrote:
i am trying to print the address of a function
This cannot be done portably. ...you may be able to store
the address in a variable, and access that variable as an array of
unsigned char, printing each element.

void (*fp)(void);
/* ... */
for (unsigned char *c = (unsigned char *) &fp; c != (unsigned char *)
(&fp + 1); c++)
printf("%x ", (unsigned) *c);

You can avoid the space(s) in the output by rounding CHAR_BIT to a
number of nibbles and printing a fixed number of hex digits for each
byte.

#define CHAR_NIBBLES ((CHAR_BIT + 3)/4)

printf("%0*x", CHAR_NIBBLES, 0u + *c);
Thanks, that's something I'll need to remember myself; it'll surely
make itself useful sooner or later. I doubt I'll use your way of
avoiding a cast, though. :)

Sep 26 '06 #25
In article <h4************ *************** *****@4ax.com>, Jack Klein
<ja*******@spam cop.netwrites
>It just plain does not define any conversions for pointers to
functions, hence any attempt to do so is undefined.
Not that it helps here but I seem to remember that it does define
conversions between different function pointer types.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Sep 26 '06 #26
In article <11************ *********@b28g2 000cwb.googlegr oups.com>,
Robert Gamble <rg*******@gmai l.comwrites
>If you examine the code snippet above, you will note that you
are converting a pointer to a function into a pointer to an object.

No, I am converting a "pointer *to a pointer* to a function" to a
"pointer to unsigned char".
You may think you are but that is not the way that pointers to functions
work. Given:

void fn(void);

fn and &fn are synonymous (how could they be anything else unless the
second expression was meaningless and Ritchie decided otherwise)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Sep 26 '06 #27
Francis Glassborow wrote:
In article <11************ *********@b28g2 000cwb.googlegr oups.com>,
Robert Gamble <rg*******@gmai l.comwrites
If you examine the code snippet above, you will note that you
are converting a pointer to a function into a pointer to an object.
No, I am converting a "pointer *to a pointer* to a function" to a
"pointer to unsigned char".

You may think you are but that is not the way that pointers to functions
work. Given:

void fn(void);

fn and &fn are synonymous (how could they be anything else unless the
second expression was meaningless and Ritchie decided otherwise)
Given:

void (*fp)(void)

fp and &fp are not synonymous, and &fp is a pointer to a pointer to a
function.

Sep 26 '06 #28
Francis Glassborow wrote:
In article <11************ *********@b28g2 000cwb.googlegr oups.com>,
Robert Gamble <rg*******@gmai l.comwrites
If you examine the code snippet above, you will note that you
are converting a pointer to a function into a pointer to an object.
No, I am converting a "pointer *to a pointer* to a function" to a
"pointer to unsigned char".

You may think you are but that is not the way that pointers to functions
work.
You are incorrect.
Given:

void fn(void);

fn and &fn are synonymous (how could they be anything else unless the
second expression was meaningless and Ritchie decided otherwise)
In this case the function type is converted to a pointer to function
type; my example was taking the address of a pointer to function
variable, not a function.

Robert Gamble

Sep 26 '06 #29
One example where the printing of a function pointer is problematic is
the PowerPC ABI, that specifies a function "descriptor " with 3 words
with the first one being the address, the second a pointer to the TOC
and the third an optional environment pointer. Casting this 3 pointers
into a void * would be a bit problematic...

jacob
Sep 26 '06 #30

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

Similar topics

3
10443
by: gudia | last post by:
I want to Programatically generate Access reports in the pdf format using some tool and then email them to people in the same code. Right now I am trying to do this with pdf995 using VBA (emailing is a separate issue, which pdf995 does not addresses). I downloaded pdf995. It controls printing via pdf995.ini file. In the VBA code, I have DoCmd.OpenReport "Report2", acViewNormal. I am having the following problems right now. (a) I do...
42
9913
by: Prashanth Badabagni | last post by:
Hi, Can any body tell me how to print "hello,world" with out using semicolon Thanks in advance .. Bye Prashanth Badabagni
4
6494
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing. When they log to application (through web browser) and choose the print option, on the right margin few cm get cut off (so some fields do not print out). Is there any function that ensure that when user pritns he gets the
4
2823
by: CsharpNewcommer | last post by:
Hi I have designed a form containing data and I want to print that form and the data as it appears in the Windows Form. I am a beginner with C# and I have read several "help"s on PrintPage, PrintDialog, etc. but none seem to address the issue of printing directly from the form. They all talk about printing from a text file. Can someone tell me how to print the form and maybe direct me on which to use PrintPage, PrintDialog....which?
7
6780
by: salmonella | last post by:
Hi, I'm trying to print on screen an address of class member function (the function of certain object of course). My code looks like this: class MyClass { public: void fun(void){}; };
9
3025
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value accordingly ? I have this doubt as different pointers may have different representation and different size. So, how does printf determine
4
2494
by: DeWittds | last post by:
I have asked before and got little responce, So I try and try again. The code below prints the data in the same place but does not advance the page. I can see the lblnumber change but print in top of the previous one. So my loop is working, I have the hasmorepages= true set right after each cycle till the loop finishes. Hopefully someone can point out my error. Thanks David DeWitt this part calls the printing code
8
5920
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web clients (Internet Explorer). My flash content was originally brought in via the “flash satay” method, but I have since used some server-side magic do deliver one <objecttag
0
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9786
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
10734
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
10836
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
10407
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
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.