0
int main()
{
    int i=21;
    char *p;
    p=(char*)&i;

    printf("%d",*p);

    getch();
    return 0;
}

printf statement gave me perfect answer but I think it shouldn't have as 'p' is a character pointer it will be able to save its base address but int takes up two spaces, *p shouldn't be able to give me integer value as it will point to address let say X but int is stored in two bytes so value need to be collected from X and X+1 address but I ran this code and gave me the value , or do I have the wrong insight on this ?

8
  • yes you have. Google "endianness" and "sign extension". Commented Aug 30, 2014 at 5:37
  • 2
    21 is in range of character or 1 byte. Increase i beyond 256. Commented Aug 30, 2014 at 5:42
  • You use %d and yet you're saying it shouldn't have printed integer value. Commented Aug 30, 2014 at 5:43
  • 2
    @rohitgoel, *p is up-converted to an int when necessary. Commented Aug 30, 2014 at 5:51
  • 2
    "I said 'p' being a character pointer should not be able to fetch you an integer value" -- 21 fits in a char just fine, so *p fetches it (on little-endian machines) ... see my answer. Commented Aug 30, 2014 at 5:54

1 Answer 1

3
p=(char*)&i;

This points p to the lowest address in i. Whether that is the address of the low order byte or the high order byte depends on the endianness of your system. (It could even be an internal byte ... PDP-11's are little-endian but longs (32 bits) were stored with the high order 16-bit word first, so the byte order was 2,3,0,1.) Likely you're running on a little-endian machine (x86's are) so it points to the low order byte.

*p

Given little-endianness, this fetches the low order byte of i, which is (char)21, and then does the default conversion to an int, giving (int)21, and prints 21. If i contained a value > 255, you would get the "wrong" result. Also if it contained a value > 127 and < 256 and char is signed on your system -- it would print a negative value.

Since the result depends on the endianness of the machine and is implementation-defined and thus is not portable, you should not do this sort of thing unless your specific goal is to determine the endianness of your machine. Beginning programmers should spend a lot less time trying to understand why bad code sometimes "works" and instead learn how to write good code. A general rule (with plenty of exceptions): code with casts is bad code.

Sign up to request clarification or add additional context in comments.

3 Comments

Here, the behavior is not undefined, only implementation-defined.
@TheParamagneticCroissant Ah, you're right; I've edited my answer accordingly. The standard allows converting a pointer to any object to a char*, which yields the lowest address of the object, and allows dereferencing it and even traversing through the object, and section 6.2.6.1 says "Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined".
@MattMcNabb "Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined." The standard guarantees the ability to process any object a char at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.