C语言中元素访问之数组

  今天在写STM32程序时,出现了一个小问题,现在记录下来,应该说是自己的C语言基本功不行。加油吧

首先,发送函数的定义:

1 void rs485_put_bytes(uint8 *buf,uint32 len)  

所访问的结构体的定义:

typedef struct
{
  uint32_t StdId;  /*!< Specifies the standard identifier.
                        This parameter can be a value between 0 to 0x7FF. */
  uint32_t ExtId;  /*!< Specifies the extended identifier.
                        This parameter can be a value between 0 to 0x1FFFFFFF. */
  uint8_t IDE;     /*!< Specifies the type of identifier for the message that will be received.
                        This parameter can be a value of @ref CAN_identifier_type */
  uint8_t RTR;     /*!< Specifies the type of frame for the received message.
                        This parameter can be a value of @ref CAN_remote_transmission_request */
  uint8_t DLC;     /*!< Specifies the length of the frame that will be received.
                        This parameter can be a value between 0 to 8 */
  uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to 0xFF. */

  uint8_t FMI;     /*!< Specifies the index of the filter the message stored in the mailbox passes through.
                        This parameter can be a value between 0 to 0xFF */
} CanRxMsg;

定义一个结构体变量:

1 CanRxMsg RxMessage;
View Code

现在rs485_put_bytes()函数要访问这个RxMessage结构体中的Data成员,第一个参数是指针,我认为下面的方法可行的,结果报错

1 rs485_put_bytes(RxMessage->Data, 8);

结果报错,

RxMessage->Data 不是指针类型

我记得是数组名是可以当做指针用的,怎么不行呢?

最后换成了

1 rs485_put_bytes(RxMessage.Data, 8);

这回就不报错了,(后来问一同事,同事说:RxMessage结构体名不能直接当做指针使用,可以使用下面的方法)

1 rs485_put_bytes((&RxMessage)->Data, 8);

将结构体名转换成指针,这样也通过编译。

可能在大部分人这都是小问题,我水平还不行,需要努力,加油,谢谢,感谢,


原文地址:https://www.cnblogs.com/craigtao/p/3269547.html