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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:44:37+00:00 2026-05-17T18:44:37+00:00

#include <stdio.h> int main(void) { int x = 1000; char *ptr = &x; printf(%d\n,*ptr);

  • 0
#include <stdio.h>

int main(void)
{ 
   int x = 1000;
   char *ptr = &x;
   printf("%d\n",*ptr);
   return 0;
 }

Output: -24 /*In gcc 4.4.3 in Ubuntu 10.04 OS*/
With a warning: initialization from incompatible pointer type

What I think if the base type of the pointer were of int type then it would have retrieved 4 bytes from the location it’s pointing.Then the O/P would have been 1000.But as I changed the base type to char then it would retrieve 1 byte from location it’s pointing when I dereference it.But how the answer is -24.
Again when I changed the program as below

#include <stdio.h>

    int main(void)
    { 
       int x = 1000;
       float *ptr = &x;
       printf("%f\n",*ptr);
       return 0;
     }

The output becomes 0.000000 with same warning.when I derefence the pointer it would retrieve 4 bytes from the location it’s pointing.But how the O/P is 0.000000.I’m bit confused.Can u guys plz explain it.I new in C programming, so any mistake in asking the question, plz forgive me. Thanx

  • 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-17T18:44:37+00:00Added an answer on May 17, 2026 at 6:44 pm

    Dereferencing a character pointer got you the first character-sized chunk of the internal representation of 1000 as an integer. That character was then promoted to a int (as per the rules for varidac arguments), and interpreted as integer.

    On you machine, with that compiler, the result was -24.

    Character sized chunk was probably a 8-bit byte.

    The integer was probably represented by 4 or 8 of those bytes.

    The internal representation was probably 2s-complement, and was probably stored in little endian order.

    Do you begin to see why the result wasn’t easy for you to predict?


    Doing the same thing with a float pointer returned a float sized chuck of memory instead of a char, and now you may have real (heh!) trouble because we don’t know, at this point if the float representation will even fit in a int representation, so you may be accessing uninitialized memory.

    In any case, when you dereferenced the float* it interpreted that memory as float (which has a rather more complex internal structure than a character or even a 2s-complement integer), and promoted it to a double (those rules for varidac arguments again) then an attempt was made interpret that representation as an integer (and if the float did fit in an int, the double probably doesn’t, so you’re interpreting part of the double as an int!). Ugh.

    This situation is even worse than the last because it involves the IEEE floating point representation standards and two chances to have the sizes not match up at all.

    So the lesson here is:

    Don’t do that.

    And the subsidiary lesson, only for experts is

    Still don’t do it.

    because if you want to perform all these silly reinterpretations you want to have exact and explicit control of them.

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

Sidebar

Related Questions

consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
#include<stdio.h> void main(){ int x,y,z; x=y=z=1; z=++x||++y&&++z; printf(%d %d %d \n,x,y,z); getch(); } the
#include stdio.h int main (void) { char xx[1000] = hello; sprintf (xx, xyzzy plugh
#include <stdio.h> const int str[1000] = {0}; int main(void) { printf(arr is %d\n, str[0]);
#include<stdio.h> #include<conio.h> int main (void) { int a,b,c,d; clrscr(); a=3; b=5; c=a,b; d=(a,b); printf(c=%d,c);
Consider the following program: #include <stdio.h> int main(void) { return 0; } When i
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include stdio.h void Square(int num, int *myPointer); int main(int argc, const char *argv[]) {
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;

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.