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 7180143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:17:11+00:00 2026-05-28T17:17:11+00:00

Possible Duplicate: Correct format specifier to print pointer (address)? When printing a pointer using

  • 0

Possible Duplicate:
Correct format specifier to print pointer (address)?

When printing a pointer using printf, is it necessary to cast the pointer to void *? In other words, in code like

#include <stdio.h>
int main() {
    int a;
    printf("address of a = %p\n", &a);
}

should the argument really be (void *) &a? gcc doesn’t seem to give any warnings when no explicit cast is made.

  • 1 1 Answer
  • 0 Views
  • 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-28T17:17:13+00:00Added an answer on May 28, 2026 at 5:17 pm

    Yes, the cast to void* is required.

    int a;
    printf("address of a = %p\n", &a);
    

    &a is of type int*; printf’s "%p" format requires an argument of type void*. The int* argument is not implicitly converted to void*, because the declaration of printf doesn’t provide type information for parameters other than the first (the format string). All arguments after the format string have the default argument promotions applied to them; these promotions do not convert int* to void*.

    The likely result is that printf sees an argument that’s really of type int* and interprets it as if it were of type void*. This is type-punning, not conversion, and it has undefined behavior. It will likely happen to work if int* and void* happen to have the same representation, but the language standard does not guarantee that, even by implication. And the type-punning I described is only one possible behavior; the standard says literally nothing about what can happen.

    (If you do the same thing with a non-variadic function with a visible prototype, so the compiler knows at the point of the call that the parameter is of type void*, then it will generate code to do an implicit int*-to-void* conversion. That’s not the case here.)

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

Sidebar

Related Questions

Possible Duplicate: Is there a printf converter to print in binary format? Consider this
Possible Duplicate: What is the correct way of using extern for global variables ?
Possible Duplicate: Asp.net how to correct the error I'm designing my web page using
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: In C, what is the correct syntax for declaring pointers? When I
Possible Duplicate: Check status of services that run in a remote computer using C#
Possible Duplicate: How can I configure Vim to compile C code using Borland’s compiler
Possible Duplicate: What is the most accurate way to retrieve a user's correct IP
Possible Duplicate: Allen Holub wrote “You should never use get/set functions”, is he correct?
Possible Duplicate: When is JavaScript’s eval() not evil? I know, generally using eval() is

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.