0

I am trying to see how endianness applies on my system. Using Python sys module, I found that its little endian, which means that least significant bytes are at lower memory addresses. So, I wanted to use C to play around this. Here is the code

int main(int argc, char *argv[])
{
    int x = 0x2e1f34;
    char *ptr = (char *) &x;
    char y = *ptr;

    printf("y = %#x\n", y);
    y = *ptr++;

    printf("y = %#x\n", y);
    y = *ptr++;

    printf("y = %#x\n", y);

    return 0;
}

Following is the output

y = 0x34
y = 0x34
y = 0x1f

First value is right. But second value should have been 0x1f as ptr has increased to the next byte address. So, using operator precedence table, pointer is increased first and then its deferenced. So, second value should be 0x1f since there is little endianness. So, what is the cause of this behavior ?

Thanks

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user9026
  • 852
  • 2
  • 9
  • 20
  • 1
    Simply don't mix `++` with other operators in the same expression and all your head aches will be gone. "as ptr has increased to the next byte address" no it hasn't, not at the point where you grab the value - how postfix vs prefix increments work and what results that are obtained from them are covered by any basic learning material. – Lundin Aug 24 '23 at 09:15
  • @Lundin great advice. will remember that always – user9026 Aug 24 '23 at 09:26

2 Answers2

3

*ptr++ gets the value and after that increments the pointer. Your code can be rewritten as:

char y = *ptr;
printf("y = %#x\n", y);
y = *ptr;
ptr++;
printf("y = %#x\n", y);

Now it should be more visible that first two y have the same value.

If you want to increment first, you could do *(++ptr) or just ptr++; y = *ptr;

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • If we use operator precedence table, then postfix increment operator has higher precedence as compared to indirection (dereference) operator. That means that pointer is increased first and then dereference is done. Is that right ? – user9026 Aug 24 '23 at 09:02
  • 1
    @user9026 The value of an expression with the postfix increment operator is the value of the operand before its incrementing. – Vlad from Moscow Aug 24 '23 at 09:08
  • 1
    @user9026 - The problem is that the *result* of the post increment is the pointer's value before it was incremented. That is what is then dereferenced. (This is really the reason for having both pre and post increment). – BoP Aug 24 '23 at 09:08
1

From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

Compare the description of the postfix increment operator with the description of the unary increment operator (the C Standard, 6.5.3.1 Prefix increment and decrement operators)

2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1).

So in both these lines

char y = *ptr;

//...

y = *ptr++;

the value of the variable y will be the same.

Instead you could write for example using unary increment operator

char y = *ptr;

//...

y = *++ptr;

Or alternatively you could write

char y = *ptr++;

//...

y = *ptr++;

Using already the postfix increment operator in the initializing expression in the declaration of the variable y.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335