2

Suppose I have:

unsafe {
    byte* start = GetStartLocation();
    int something = start[4];
}

What is something? The value of the memory address 4 bytes down from start?

3 Answers 3

6

Say start points to memory location 0x12345678, and memory looks like this:

  0x12345678   0x12
  0x12345679   0x34
  0x1234567a   0x56
  0x1234567b   0x78
  0x1234567c   0x9a
  0x1234567d   0xbc

then something equals 0x9a.

The fact that something has type int doesn't matter to how start[4] is interpreted - it gets the byte value of the byte 4 locations away from start.

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

2 Comments

Thank you! I need to enter 15 characters to post this, which leads me to believe that 'thank you's in general are not allowed, which means this will be deleted shortly, so I hope you see this! Thanks!
@Sam: Personally, I think the more "thank you"s on SO the better. Feel free to pad them with smileys as necessary. 8-)
2

The value of something is the byte value of offset 4 from start widened to an int type.

It's equivalent to the following code

byte temp = start[4];
int something = temp;

Comments

2

start[4] will evaluate to:

*(start + 4)

Comments

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.