Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I access members of a struct when it's not aligned properly?
    primarykey
    data
    text
    <p>I'm afraid that I'm not very good at low level C stuff, I'm more used to using objects in Obj-c, so please excuse me if this is an obvious question, or if I've completely misunderstood something...</p> <p>I am attempting to write an application in Cocoa/Obj-C which communicates with an external bit of hardware (a cash till.) I have the format of the data the device sends and receives - and have successfully got some chunks of data from the device.</p> <p>For example: the till exchanges PLU (price data) in chunks of data in the following format: (from the documentation)</p> <p>Name Bytes Type</p> <pre><code>Name Bytes Type PLU code h 4 long PLU code L 4 long desc 19 char Group 3 char Status 5 char PLU link code h 4 long PLU link code l 4 long M&amp;M Link 1 char Min. Stock. 2 int Price 1 4 long Price 2 4 long Total 54 Bytes </code></pre> <p>So I have a struct in the following form in which to hold the data from the till:</p> <pre><code>typedef struct MFPLUStructure { UInt32 pluCodeH; UInt32 pluCodeL; unsigned char description[19]; unsigned char group[3]; unsigned char status[5]; UInt32 linkCodeH; UInt32 linkCodeL; unsigned char mixMatchLink; UInt16 minStock; UInt32 price[2]; } MFPLUStructure; </code></pre> <p>I have some known sample data from the till (below) which I have checked by hand and is valid </p> <p>00 00 00 00 4E 61 BC 00 54 65 73 74 20 50 4C 55 00 00 00 00 00 00 00 00 00 00 00 09 08 07 17 13 7C 14 04 00 00 00 00 09 03 00 00 05 BC 01 7B 00 00 00 00 00 00 00</p> <p>i.e. </p> <ul> <li><p>bytes 46 to 50 are <code>&lt;7B 00 00 00&gt;</code> == 123 as I would expect as the price is set to '123' on the till.</p></li> <li><p>byte 43 is <code>&lt;05&gt;</code> == 5 as I would expect as the 'mix and match link' is set to 5 on the till. </p></li> <li><p>bytes 39 to 43 are <code>&lt;09 03 00 00&gt;</code> == 777 as I would expect as the 'link code' is set to '777' on the till.</p></li> <li><p>Bytes 27,28,29 are <code>&lt;09 08 07&gt;</code> which are the three groups (7,8 &amp; 9) that I would expect.</p></li> </ul> <p>The problem comes when I try to get some of the data out of the structure programmatically: The early members work correctly right up to, and including the five 'status' bytes. However, members after that don't come out properly. (see debugger screenshot below.) </p> <p>Image 1 - <a href="http://i.stack.imgur.com/nOdER.png">http://i.stack.imgur.com/nOdER.png</a></p> <p>I assume that the reason for this is because the five status bytes push later members out of alignment - i.e. they are over machine word boundaries. Is this right?</p> <p>Image 2 - i.imgur.com/ZhbXU.png</p> <p>Am I right in making that assumption?<br> And if so, how can I get the members in and out correctly?</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload