1

This is a fully working code and there is only one line of this code that confused me and I don't understand that.

How and why cp[1] = 0 and from what I understood it should be 01 or?

#include <stdio.h>

int main(void){
char *cp;
short *sp;
int *ip;
short x[6];
int i, y;
y = 0x0102;
printf("y = %x\n" , y);
for(i = 0; i < 6; i++){
x[i] = y; y = y +0x1010;
}
printf("y = %x\n" , y);
printf("x[0] = %x\n" , x[0]);
printf("x[1] = %x\n" , x[1]);
printf("x[2] = %x\n" , x[2]);
printf("x[3] = %x\n" , x[3]);
printf("x[4] = %x\n" , x[4]);
printf("x[5] = %x\n" , x[5]);
cp = (char*) x;
printf("1)*cp = %x\n" , *cp);
sp = x;
printf("2)*sp = %x\n" , *sp);
printf("3)*cp[3] = %x\n" , cp[3]);
printf("3)*cp[6] = %x\n" , cp[6]);

ip = (int*) x;
printf("A)*ip = %x\n" , *ip);

ip = ip +1;
printf("B)*ip+1 = %x\n" , *ip);
printf("C)*cp[6] = %x\n" , cp[6]);

sp = sp +5;
printf("D)*sp +5 = %x\n" , *sp);

//change the content of x
*x = *cp + 2;

printf("E)*cp[1] = %x\n" , cp[1]);
printf("F)*cp[0] = %x\n" , cp[0]);

return 0;
}

output:

y = 102
x[0] = 102
x[1] = 1112
x[2] = 2122
x[3] = 3132
x[4] = 4142
x[5] = 5152
1)*cp = 2
2)*sp = 102
3)*cp[3] = 11
3)*cp[6] = 32
A)*ip = 11120102
B)*ip+1 = 31322122
C)*cp[6] = 32
D)*sp +5 = 5152
E)*cp[1] = 0
F)*cp[0] = 4
Adam
  • 856
  • 2
  • 9
  • 18
  • and why do you think it should be 01 ? – asio_guy Aug 30 '18 at 10:13
  • 1
    It's probably an [*endianness*](https://en.wikipedia.org/wiki/Endianness) problem. What is the value (in hex) of `x[0]`? – Some programmer dude Aug 30 '18 at 10:13
  • Also, the program that you show isn't really a [**Minimal**, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). It could be shortened quite considerably. – Some programmer dude Aug 30 '18 at 10:14
  • @Someprogrammerdude `x[0] = 0x0102` – Adam Aug 30 '18 at 10:16
  • @u__ I thought that since `*x = *cp +2; ` which is equivalent to `*x = 0x0102+2= 0x0104` and then `cp[1] ` points to second byte there which should be `01`. – Adam Aug 30 '18 at 10:22
  • @someprogrammerdude no it can't be simplified because the OP had no idea what's going on. Please don't expect those asking questions to know the answer to their question. – Philip Couling Aug 30 '18 at 10:24
  • 1
    Your code has *strict aliasing violation* (and possible alignment issue) because you are dereferencing `short` type through `int*` pointer. This results in *undefined behaviour*, and may result in compiler emitting incorrect output. Please remove these violations (basically usage of `ip`) before attempting to decipher the code. – user694733 Aug 30 '18 at 10:33
  • No so `&cp[0] = &x[1]` – Philip Couling Aug 30 '18 at 10:35
  • 2
    This code is overall buggy and bad. Don't look at it, there is nothing to learn from it except how to write bugs and make programs non-portable. Whoever wrote it was an amateur who should not be teaching C. `ip = (int*) x; printf("A)*ip = %x\n" , *ip);` is for example a strict aliasing violation, an undefined behavior bug. – Lundin Aug 30 '18 at 10:43

2 Answers2

1

In much the same way that x[0] is being displayed as 102 rather than 0102, you need to tell printf how many digits you want printed as it won't print out leading 0's otherwise. So to get a minimum of 2 digits printed with leading 0's you'd change the %x to %02x or in the case of x[0] you'd put %04x for a minimum of 4 digits.

printf("x[0] = %04x\n" , x[0]);
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
1

My answer assumes that a short has size 2. I am also ignoring the fact that the code breaks the strict aliasing rule (turn up the compiler optimization level and the output may change).

//change the content of x
*x = *cp + 2;   // this changes *x to 0x0004

*x has size 2 on your system, so assigning it will modify both cp[0] and cp[1].

Thus, the expected output is either

E)*cp[1] = 0
F)*cp[0] = 4

or

E)*cp[1] = 4
F)*cp[0] = 0

depending on the endianness of the system.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82