Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 558693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:09:32+00:00 2026-05-13T12:09:32+00:00

I want to print out a function pointer using cout , and found it

  • 0

I want to print out a function pointer using cout, and found it did not work.

But, it worked after I converted the function pointer to void*, so does printf with %p, such as:

#include <iostream>
using namespace std;

int foo() {return 0;}

int main()
{
    int (*pf)();
    pf = foo;
    cout << "cout << pf is " << pf << endl;
    cout << "cout << (void *)pf is " << (void *)pf << endl;
    printf("printf(\"%%p\", pf) is %p\n", pf);
    return 0;
}

I compiled it with g++ and got results like this:

cout << pf is 1  
cout << (void *)pf is 0x100000b0c  
printf("%p", pf) is 0x100000b0c

So, what does cout do with type int (*)()? I was told that the function pointer is treated as bool, is it true?

And, what does cout do with type void*?


EDIT: Anyhow, we can observe the content of a function pointer by converting it into void* and printing it out using cout. But, it does not work for member function pointers, and the compiler complains about an illegal conversion. I know that member function pointers is rather a complicated structure other than simple pointers, but how can we observe the content of a member function pointer?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:09:32+00:00Added an answer on May 13, 2026 at 12:09 pm

    There actually is an overload of the << operator that looks something like:

    ostream & operator <<( ostream &, const void * );
    

    which does what you expect – outputs in hex. There can be no such standard library overload for function pointers, because there are infinite number of types of them. So the pointer gets converted to another type, which in this case seems to be a bool – I can’t offhand remember the rules for this.

    Edit: The C++ Standard specifies:

    4.12 Boolean conversions

    1 An rvalue of arithmetic,
    enumeration, pointer, or pointer to
    member type can be converted to an
    rvalue of type bool.

    This is the only conversion specified for function pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your two modules are so tightly coupled, wouldn't it… May 13, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer It's possible to add a ToolTip to an existing control.… May 13, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer You really need to keep two issues apart: 1) the… May 13, 2026 at 8:26 pm

Related Questions

EDIT: I have found the error: I did not initialize an array with a
A class project involves sorting an array of strings, with each string containing an
I'm developing a SOAP application that integrates with a 3rd party. I think the
I'm trying to print out a DataGridView using a PrintDocument. In my PrintPage event
I have a situation where I have an interface that defines how a certain

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.