0

I'm reading post about pointers in C here>>>, and there is one example:

main() {
    ...
    char name[] = "Bill";
    int *q;
    ...
    q = name;
    printf("%d\n", *q);
    ...

Which gives me next result:

$ ./pointers_explained 
1819044162

So question is about next explanation from author:

(to see how, line up the binary representation of the ascii values for those 4 characters, and then run the 32 bits together, and convert that resultant binary number as an integer.)

Binary for the "Bill" will be:

  • B: 01000010
  • i: 01101001
  • l: 01101100
  • l: 01101100

(from an ASCII binary table here>>>)

What I can't get is:

and then run the 32 bits together, and convert that resultant binary number as an integer

So - how can I convert this "Bill"'s binary into the decimal (or any other ) integer "1819044162"?

2
  • 4
    note that this is undefined behaviour, the "explanation" is details of some particular compiler and system Commented May 22, 2016 at 8:28
  • @M.M Yeah, thanks for the clarification, just wondering about how it's working in total. Commented May 22, 2016 at 8:40

1 Answer 1

3

In ascii code, "Bill" is

B: 0x42
i: 0x69
l: 0x6c
l: 0x6c

1819044162 in hex mode is 0x6c6c6942, this is because of the endianness

In memory, "Bill" is stored as:

     B   i   l   l
name 42  69  6c  6c

But if it is little endianness, and reads an 4-byte integer from address name, it reads backwards, results 0x6c6c6942, which is 1819044162.

If the system is big endian, it will results 0x42696c6c.

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

1 Comment

Nice explanation, but it is good to say that this can have alignment problems.

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.