Note that there are some explanatory texts on larger screens.

plurals
  1. POIn C, a pointer to a structure that needs to access a list of byte pointers
    text
    copied!<p>First off, sorry for the title, it was hard to come up with a description. I am attempting to create a structure that will access a peripheral that has quite of few internal registers that are controlled by 3 address lines. So I have the following structure definition:</p> <pre><code>typedef struct { union { unsigned char *reg0aPtr; unsigned char *reg0bPtr; } RegsAddrOffset0; union { unsigned char *reg1aPtr; unsigned char *reg1bPtr; } RegsAddrOffset1; ... union { unsigned char *reg7aPtr; unsigned char *reg7bPtr; } RegsAddrOffset7; } DeviceRegMap; </code></pre> <p>Then if I declare a variable as so</p> <pre><code>DeviceRegMap *uartRegMap = (DeviceRegMap *)(0xD0000000); </code></pre> <p>Now, if I try to to use the uartRegMap variable to access the memory mapped registers I thought I would see that *(uartRegMap->RegsAddrOffset0.reg0aPtr) would read the data from address 0xD0000000, which is does. If I use *(uartRegMap->RegsAddrOffset1.reg1bPtr) I thought I would access 0xD0000001, but it is actually accesses address 0xD0000004. Since the processor is 32-bit the natural alignment is 4-bytes. I am thinking since the size of a pointer is an int which in this case is 32-bits this is the reason why I am accessing on 4-byte address ranges (e.g., 0xD0000000, 0xD0000004, 0xD0000008, etc).</p> <p>If I declared an instance of the DeviceRegMap datatype I would expect the following initialization:</p> <pre><code>DeviceRegMap uartRegMap = { (unsigned char *)0xD0000000, (unsigned char *)0xD0000001, ... (unsigned char *)0xD0000007, }; </code></pre> <p>Then if I wanted to access address 0xD0000007 then I would use *(uartRegMap.RegsAddrOffset7.reg7bPtr)</p> <p>The question is why is the pointer to the DeviceRegMap data type access memory on 4-byte alignments (e.g., 0xD0000000, 0xD0000004, etc) instead of byte alignments (e.g., 0xD0000000, 0xD0000001, etc). With regards to the toolset I am using Wind River diab compiler. Thanks.</p> <p>Mark</p> <p>EDIT: Based upon abelenky's comment</p>
 

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