<分区>
我是一名高中生,最近使用 Arduino 开始了我的期末考试项目,我试图理解我在使用 VirtualWire 的 radio 发射器示例草图中找到的这段代码。
const char *msg = "hello";
vw_send((byte *)msg, strlen(msg));
我从来没有学过指针甚至 C,所以我在理解它的工作原理时遇到了一些问题。
*msg
是指向 char
的指针,对吧?到那时,它应该是空的,那你为什么可以给它赋值呢?
此外,(byte *)msg
到底是什么鬼东西?这些括号是什么意思?它是一个 byte
等于指针 msg
处的值吗?考虑到它是一个 char
,它是如何工作的?为什么有必要?
上下文的完整代码:
// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop()
{
const char *msg = "hello";
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((byte *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(200);
}