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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:01:27+00:00 2026-06-12T00:01:27+00:00

I have a code which goes like : int main(void) { char mychar =

  • 0

I have a code which goes like :

int main(void)
{
    char mychar = 'd';
    int *ptr = malloc(sizeof(*ptr)) ;

    *ptr = (char) 'c' ; // *ptr = (char*) 'c'; Gives the exact same result

    printf("%c\n",*ptr);
    memset(ptr,mychar,sizeof(*ptr));
    printf("%c\n",*ptr);
    free(ptr);
    printf("%c\n",*ptr);
    return 0 ;
}

The code gives the same result for pointer style cast and datatype cast.
Which among the two styles should be used or is considered a good practice and why ?

*ptr = (char) 'c' ; 
*ptr = (char*) 'c';
  • 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-06-12T00:01:28+00:00Added an answer on June 12, 2026 at 12:01 am

    Neither.

    Both *ptr and 'c' are of type int. No cast is necessary.

    (Yes, character constants in C are of type int, not char. This is counterintuitive, but there are historical reasons for it. Note that C++ character constants are of type char.)

    The cast in *ptr = (char) 'c'; is relatively harmless; it converts the int value of 'c' to char, and the result is implicitly converted back to int. Since c is guaranteed to be representable as a char, it’s not going to cause an error.

    In *ptr = (char*) 'c';, your compiler should give you an error message. You’re converting a constant int value to a pointer, and then assigning that pointer value to an int without a cast. This is a “constraint violation”, which means that a conforming compiler must diagnose it. If yours doesn’t, find out how to make it do so.

    If the assignment gets past the compiler, it might do what you want. Conversions between integer types and pointer types typically just reinterpret the bits that name up the representation, so converting an int to a char* and back to an int is likely to give you the original int value. But it’s not guaranteed.

    Casts are usually unnecessary even between different arithmetic types. For example, if you had written:

    char ch = 'c';
    int i;
    i = ch;
    

    In this case, the right side of the assignment really is of type char, because it’s the name of an object of that type, not a character constant. The assignment implicitly converts the value of ch from type char to type int. You could write:

    i = (int)ch;
    

    instead, but that just specifies the same conversion that would have been done without the cast. And it’s error-prone; it introduces the opportunity to get the type wrong, as opposed to letting the compiler handle it for you.

    Casts in general should be viewed with suspicion.

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

Sidebar

Related Questions

I have the following line of code which when compiled with GHC it goes
I have code which needs to do something like this There is a list
I have code which looks like: private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
Theres a bit of C code something like: int doCommand(char* command) { // +2
I have code which has a drop down list. And when a certain option
I have code which as been working against an older Active Directory server and
I have code which performs query on Jboss server. It has JNDI based datasource
I have code which works in all sorts of different situations, including when copying
I already have code which lazy loads scripts on request. My issue now is
Hi I have code which sets up a basic client to server scenario and

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.