我被告知(特别是在对 Arduino 上的 C++ 标准库的回答中,以及在 Stack Overflow 问题C++ 字符串和 Arduino 字符串中。如何组合它们?))Arduino 编译器没有实现new
运算符。但是,我为使用它的 Arduino(在 Arduino IDE 中)编写了一个程序,它运行良好。
void setup() {
Serial.begin(9600);
}
void loop() {
char* array;
char c;
unsigned arraySize;
Serial.write("Enter a 1 digit number.\n");
do {
c = Serial.read();
} while(c < '0' or c > '9');
arraySize = c-'0';
Serial.write("You wrote ");
Serial.write(c);
Serial.write(".\n");
Serial.write("Now enter ");
Serial.write(c);
Serial.write(" lower-case letters.\n");
array = new char[arraySize];
for (unsigned i = 0; i < arraySize;) {
array[i] = Serial.read();
if (array[i] >= 'a' and array[i] <= 'z')
i++;
}
Serial.write("You entered: ");
for (unsigned i = 0; i < arraySize; i++) {
Serial.write(array[i]);
Serial.write(" ");
}
Serial.write("\n");
}
以下是演示其功能的示例输出:
Enter a 1 digit number.
You wrote 5.
Now enter 5 lower-case letters.
You entered: h e l l o
Enter a 1 digit number.
You wrote 9.
Now enter 9 lower-case letters.
You entered: w a s s u p m a n
Enter a 1 digit number.
You wrote 9.
Now enter 9 lower-case letters.
You entered: h o w y a d o i n
Enter a 1 digit number.
You wrote 4.
Now enter 4 lower-case letters.
You entered: c o o l
Enter a 1 digit number.
You wrote 7.
Now enter 7 lower-case letters.
You entered: i t w o r k s
Enter a 1 digit number.
那为什么我一直听到这个?这些人是错的,还是我只是误解了他们的意思?