Puzzling program



Good day group.

I was asked in an interview to explain the behavior of this program.

void main()
{
char *s = "abc";
int *i = (int *) s;
printf("%x", *i);
}

The question is: why is the output 636261? I don't think I know enough
about C to understand how the conversions between pointer types are
occurring.

Thanks for any help.

RD

.



Relevant Pages

  • Re: What is a null pointer constant?
    ... Perhaps what I'd do instead is define two void* ... > be OK but conversions back to other types would have to be explicit. ... > other pointer types, but requiring an explicit cast in the context of a ...
    (comp.std.c)
  • Re: Puzzling program
    ... I was asked in an interview to explain the behavior of this program. ... void main ... about C to understand how the conversions between pointer types are ... Free games and programming goodies. ...
    (comp.lang.c)
  • Re: Puzzling program
    ... I was asked in an interview to explain the behavior of this program. ... about C to understand how the conversions between pointer types are ... I'd never like to work for a company where interviewers declare main ... as a void function... ...
    (comp.lang.c)
  • Re: Puzzling program
    ... I was asked in an interview to explain the behavior of this program. ... The illiterate return type 'void' for main makes all bets off. ... and understanding the probable explanation for the ...
    (comp.lang.c)
  • Re: What is a null pointer constant?
    ... But I like the idea of enforcing the safety check. ... Perhaps what I'd do instead is define two void* types ... conversions back to other types would have to be explicit. ...
    (comp.std.c)