This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

casting pointers

Hello!

I've bumped into the issue with casting...

main.c:
static ble_gap_addr_t addr0;
static uint16_t *p_host0;

int main(void) {
    p_host0 = (void*)&addr0.addr[0];

works fine, but...

beacon.c:
static ble_gap_addr_t addr1;
static uint16_t *p_host1;

void init_beacon(void) {
   p_host1 = (void*)&addr1.addr[0];

hungs in assignment when I call init_beacon() from main()

Where is rake?

  • Stan, it seems to me that you have misunderstood the C pointer. Your way of assigning and casting of array elements doesn't fulfill your intension. The p_host1 of your code holds not values of addr but the address of addr. To hold values of array elements you should use memcpy.

  • definitely it is:

    **Cortex-M0 Devices Generic User Guide

    Home > The Cortex-M0 Instruction Set > About the instruction descriptions > Address alignment

    3.3.4. Address alignment

    An aligned access is an operation where a word-aligned address is used for a word, or multiple word access, or where a halfword-aligned address is used for a halfword access. Byte accesses are always aligned. There is no support for unaligned accesses on the Cortex-M0 processor. Any attempt to perform an unaligned memory access operation results in a HardFault exception.**

  • p_host1 holds the address of the value, I know, so when I need the value, I use *p_host1 - dereference it. I just tried to avoid copying of memory as much as possible...

  • Oh I see. Then you can assign the pointer of uint8_t like this:

    static uint8_t *p_host1;
    void init_beacon(void) {
        p_host1 = &addr1.addr[0];
    }
    

    then use it like array, p_host1[0], p_host1[1]...

  • it will surely work, but slightly estrange me from the goal. initial purpose of using 16 bit pointer is simplification of code. What I need, is to distinguish a subset of addresses, which may consist of more than 255 ones (a resolution, provided by one octet). So I decided to use 48 bit address, expressed by uint8_t array, in manner of IP: a "network mask" part, as higher 32 bits (conveniently accessed by uint32_t pointer) and "host" part - remaining 16 bits for . So not to bother myself with traveling across 8-bit array, proposed solution had been chosen. And it is true, that the issue is solely alignment related, so, respecting the alignment restrictions of ARM arch., it will work.

Related