Whats going on with accessing my array via a pointer?

Hi,

Please forgive me if I am asking a silly question but I am trying to understand what the following code is doing as I am trying to convert it to delphi and cant replicate the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  typedef unsigned char byte;
  typedef unsigned short int u16;

  byte tester_byte[75+1];

  u16 *tester;
  u16 tmp;

  tester_byte[0]=0x00;
  tester_byte[1]=0x00;
  tester_byte[2]=0xc1;
  tester_byte[3]=0xc0;
  tester_byte[4]=0x81;
  tester_byte[5]=0xc1;
  tester_byte[6]=0x40;
  tester_byte[7]=0x01;
  tester_byte[8]=0x01;
  tester_byte[9]=0xc3;
  tester_byte[10]=0xc0;
  tester_byte[11]=0x03;
  tester_byte[12]=0x80;
  tester_byte[13]=0x02;
  tester_byte[14]=0x41;
  tester_byte[15]=0xc2;
  tester_byte[16]=0x01;
  tester_byte[17]=0xc6;
  tester_byte[18]=0xc0;
  tester_byte[19]=0x06;
  tester_byte[20]=0x80;
  tester_byte[21]=0x07;
  tester_byte[22]=0x41;
  tester_byte[23]=0xc7;
  tester_byte[24]=0x00;
  tester_byte[25]=0x05;
  tester_byte[26]=0xc1;
  tester_byte[27]=0xc5;
  tester_byte[28]=0x81;
  tester_byte[29]=0xc4;
  tester_byte[30]=0x40;
  tester_byte[31]=0x04;
  tester_byte[32]=0x01;
  tester_byte[33]=0xcc;
  tester_byte[34]=0xc0;
  tester_byte[35]=0x0c;
  tester_byte[36]=0x80;
  tester_byte[37]=0x0d;
  tester_byte[38]=0x41;
  tester_byte[39]=0xcd;
  tester_byte[40]=0x00;
  tester_byte[41]=0x0f;
  tester_byte[42]=0xc1;
  tester_byte[43]=0xcf;
  tester_byte[44]=0x81;
  tester_byte[45]=0xce;
  tester_byte[46]=0x40;
  tester_byte[47]=0x0e;
  tester_byte[48]=0x00;
  tester_byte[49]=0x0a;
  tester_byte[50]=0xc1;
  tester_byte[51]=0xca;
  tester_byte[52]=0x81;
  tester_byte[53]=0xcb;
  tester_byte[54]=0x40;
  tester_byte[55]=0x0b;
  tester_byte[56]=0x01;
  tester_byte[57]=0xc9;
  tester_byte[58]=0xc0;
  tester_byte[59]=0x09;
  tester_byte[60]=0x80;
  tester_byte[61]=0x08;
  tester_byte[62]=0x41;
  tester_byte[63]=0xc8;
  tester_byte[64]=0x01;
  tester_byte[65]=0xd8;
  tester_byte[66]=0xc0;
  tester_byte[67]=0x18;
  tester_byte[68]=0x80;
  tester_byte[69]=0x19;
  tester_byte[70]=0x41;
  tester_byte[71]=0xd9;
  tester_byte[72]=0x00;
  tester_byte[73]=0x1b;
  tester_byte[74]=0xc1;
  tester_byte[75]=0xdb;

  tester = (u16*)tester_byte;

  tmp = * (tester + 37);


in the above code tmp has a value of 56257. How does that value come about? My understanding is that tmp should have a value that matches the 37th entry in the array, in this case 13 (0d).

Thanks for your time.

Jay.
You have an array of bytes (char) but you are trying to access it like an array of u16 (short int). These probably aren't the same size so your pointer arithmetic won't be the same.
Putting aside the disregard for pointer aliasing and the non-guaranteed size of the short, note that 56257 is 0xdbc1, that is, your pointer is pointing at the byte number 74 (and you machine appears to be little-endian)

Since the type of 'tester' is u16*, it is treated by operator+ as an iterator for an array of u16's. By definition, adding a number n to the pointer to the i'th element of an array results in a pointer to the i+n'th element of the same array. The size of the short in your case is indeed 2 bytes, so the (0+37)'th element of the imaginary array of u16's begins at byte 74.
Last edited on
well 56257 is dbc1 in hex.

so.

tester is of type uint16 whereas the array is in bytes so tester+37 jumps 37 uint16s (which are each two bytes long) up the array (to position 74)

dereferencing as a uint16 takes two consecutive bytes as a short int, so these are the end ones db,c1

no idea of the point of doing this
Thank you all very much, I now understand whats going on and how the values are being calculated.

This code is very old and untouched in a long time...long before I started working here and the bit you are seeing is only an extract that has been simplified to show my problem and is perhaps obsolete now, but until I can understand it I cant really say for sure.

Thanks again guys.
Topic archived. No new replies allowed.