cancel
Showing results for 
Search instead for 
Did you mean: 

Accesing the field of a struct when it is a pointer?

john doe
Lead
Posted on April 20, 2017 at 19:39

Having trouble reading the MAC address using the HAL but mostly because of my inexperience.

 uint8_t             *MACAddr;                   /*!< MAC Address of used Hardware: must be pointer on an array of 6 bytes */

printing heth.Init.MACAddr[0] does not print the first byte. It prints something, but not what I am expecting.  if I send &heth.Init.MACAddr[0] to printf, it does print the address, but the first try without the & produces what seems to be a different address. The value does not change no matter what I set the MAC Address to.  I'm obviously missing something when it comes to reading the value of a field of a struct when its a pointer.  Can someone help me get on the right path?

      len += sprintf(etherString+len, ' MAC Address      : %02X:%02X:%02X:%02X:%02X:%02X\r\n',

              heth.Init.MACAddr[0], heth.Init.MACAddr[1], heth.Init.MACAddr[2], heth.Init.MACAddr[3],

              heth.Init.MACAddr[4], heth.Init.MACAddr[5]);

produces

 MAC Address      : 84:00:00:20:2B:39

the mac address is actually 00:80:E1:00:00:00

what am I not getting?

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on April 20, 2017 at 20:25

Kind of suggests that MACAddr isn't set to anything, or is set to a transient address for a local/auto variable, that's out of scope at the point of printing.

ie

heth.Init.MACAddr = malloc(6);

heth.Init.MACAddr[0] = 0x11;

heth.Init.MACAddr[1] = 0x22;

heth.Init.MACAddr[2] = 0x33;

heth.Init.MACAddr[3] = 0x44;

heth.Init.MACAddr[4] = 0x55;

heth.Init.MACAddr[5] = 0x66;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4
Alex R
Senior
Posted on April 20, 2017 at 20:21

Hi,

Note that MACAddr is a pointer, so it doesn't actually contain the data for the MAC address.

How do you assign (write) the MAC address into the Init data structure?

Regards,

Alex.

Posted on April 20, 2017 at 20:25

Kind of suggests that MACAddr isn't set to anything, or is set to a transient address for a local/auto variable, that's out of scope at the point of printing.

ie

heth.Init.MACAddr = malloc(6);

heth.Init.MACAddr[0] = 0x11;

heth.Init.MACAddr[1] = 0x22;

heth.Init.MACAddr[2] = 0x33;

heth.Init.MACAddr[3] = 0x44;

heth.Init.MACAddr[4] = 0x55;

heth.Init.MACAddr[5] = 0x66;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Posted on April 20, 2017 at 21:03

Find in file how the code is using or declaring 

MACAddr in the HAL.

Human commented text won't be as accurate as C...

If using debug mode, open the structure contents and use the memory watch window to make sure everything is pointing as expected.

Posted on April 21, 2017 at 02:49

nailed it, once again. The HAL never updates that field of the struct anyway and as

Centauris.Alpha

suggested, I dug up how the MAC address is configured and it turns out the HAL just writes to the registers and, for a change, doesn't update the struct like it does for everything else. I was also confusing which struct was being returned from the call to get its status.

this is how I ended up reading the MAC address on an stm32f769i-discovery board:

uint32_t htempreg;

uint32_t ltempreg;

uint8_t macAddr[6];

htempreg = (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_HBASE)));

ltempreg = (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_LBASE)));

macAddr[0] = (ltempreg) & 0xFF;

macAddr[1] = (ltempreg >> 😎 & 0xFF;

macAddr[2] = (ltempreg >> 16) & 0xFF;

macAddr[3] = (ltempreg >> 24) & 0xFF;

macAddr[4] = (htempreg) & 0xFF;

macAddr[5] = (htempreg >> 😎 & 0xFF;

len += sprintf(etherString+len, ' MAC Address : %02X:%02X:%02X:%02X:%02X:%02X\r\n',

macAddr[0],macAddr[1],macAddr[2],macAddr[3],macAddr[4],macAddr[5]);