我正在学习编程嵌入式以太网,我遇到了一段代码问题:
uint8 emacAddress[6U] = {0x00U, 0x08U, 0xEEU, 0x03U, 0xA6U, 0x6CU};
uint32 emacPhyAddress = 0U;
int main(void)
{
hdkif_t hdkif01;
hdkif01.mac_addr[6] = emacAddress;
当我构建代码时,我得到关于最后一行的两个警告:
“hdkif_t”定义为:
typedef struct hdkif_struct {
/* MAC Address of the Module. */
uint8_t mac_addr[6];
/* emac base address */
uint32 emac_base;
/* emac controller base address */
volatile uint32 emac_ctrl_base;
volatile uint32 emac_ctrl_ram;
/* mdio base address */
volatile uint32 mdio_base;
/* phy parameters for this instance - for future use */
uint32 phy_addr;
boolean (*phy_autoneg)(uint32 param1, uint32 param2, uint16 param3);
boolean (*phy_partnerability)(uint32 param4, uint32 param5, uint16* param6);
/* The tx/rx channels for the interface */
txch_t txchptr;
rxch_t rxchptr;
}hdkif_t;
emacAddress的正确数据类型是什么?为什么?
答案 0 :(得分:1)
您的语句hdkif01.mac_addr[6] = emacAddress;
正在将指向数组emacAddress
的指针分配给hdkif01.mac_addr
的第七个元素。请记住,数组索引在C中从零开始。
由于hdkif01.mac_addr
是uint8_t
的数组,因此第七个元素(A)不存在,而(B)不足以容纳指针。 (B)是你错误的原因。
看起来您正在尝试复制数组。您有一些选项,从大多数手册到最少列出:
逐个元素复制,没有循环:
hdkif01.mac_addr[0] = emacAddress[0];
hdkif01.mac_addr[1] = emacAddress[1];
...
hdkif01.mac_addr[5] = emacAddress[5];
循环复制元素:
int i;
...
for(i = 0; i < 6; i++)
hdkif01.mac_addr[i] = emacAddress[i];
使用功能:
#include <string.h>
...
memcpy(hdkif01.mac_addr, emacAddress, sizeof(emacAddress));