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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:18:58+00:00 2026-05-27T03:18:58+00:00

I believed that in the following code, C automatically casts 17 to an int

  • 0

I believed that in the following code, C “automatically casts 17 to an int *” which, as someone recently pointed out (but did not give the reasons as to why), is wrong.

int *ptoi = 17; // I assumed that 17 is being automatically casted to int *

I know that if I do the same thing as above in C++, I get an error saying invalid conversion from int to int *. But if I do the following in C++, it works fine:

int *ptoi = (int *)17;

These are the reasons I thought that in C, the casting was implicit.

Can someone please explain why, in C++, I have to cast it but in C, it works fine?

  • 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-27T03:18:58+00:00Added an answer on May 27, 2026 at 3:18 am

    Conversions from integers to pointers without casts are also illegal in C. Most compilers will let you get away with it though. Clang gives a warning:

    example.c:5:8: warning: incompatible integer to pointer conversion initializing
          'int *' with an expression of type 'int'
      int *x = 17;
           ^   ~~
    

    C99 says in Section 6.5.4 Cast operators, paragraph 4:

    Conversions that involve pointers, other than where permitted by the constraints of 6.5.16.1, shall be specified by means of an explicit cast.

    6.5.16.1 is the exception for void * converting to other pointers without needing a cast.

    The C++ spec says in Section 5.4 Explicit type conversion (cast notation), paragraph 3:

    Any type conversion not mentioned below and not explicitly defined by the user is ill-formed.

    So there you go – illegal in both languages, but for compatibility with lots of older software, a lot of C compilers will let you get away with it.

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

Sidebar

Related Questions

I'm a beginner w/ Javascript. I'm looking at the following code that someone else
Until recently, I believed that the .NET runtime only increases the reference count of
in the following code: int utf8len(char* s, int len) { Glib::ustring::iterator p( string::iterator(s) );
I wrote the following code for a project that I'm working on: var clicky_tracking
I just discovered that the following code executes without error (Chrome): console.log(fa); function fa(){}
I have the following code in a partial that I am trying to convert
There is the following definition in some legacy code that I am working with.
http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html I believe that the following code increases the value of var atomically. volatile
Hy all, I believe that the following piece of code is generating memory leak?
Given the following code which works: for (i=0; i<nLinears; i++) { for (j=0; j<nLinearPts[i]-1;

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.