简体   繁体   English

一元'*'的无效类型参数

[英]Invalid type argument of unary ‘*’

I have problem with my Arduino C++ code. 我的Arduino C ++代码有问题。 Here is function: 这是功能:

  void sendDeviceName(){
  char buffer[3] = "";
  incomingCommand.toCharArray(buffer, 3);
  int deviceNumber = atoi(*buffer[2]);
  Serial.println(EEPROMreadDevice(deviceNumber));
}

When I'm trying compile my code compiler returns: 当我尝试编译我的代码时,编译器返回:

error: invalid type argument of unary '*' 错误:一元'*'的类型参数无效

I tried to fix it yourself, but I do not go. 我试图自己修复它,但我没有去。

The error comes from the fact that buffer[2] is a char , not a pointer. 该错误来自以下事实: buffer[2]char ,而不是指针。 There is nothing to dereference here. 这里没有要取消引用的内容。 If you are trying to turn a char representing a digit into the corresponding int value use: 如果试图将代表数字的char转换为相应的int值,请使用:

int deviceNumber = buffer[2] - '0';

Or generally if you want the last NK chars of a char array use: 或者通常,如果要使用char数组的最后NK个char使用:

int deviceNumber = atoi(buffer + K);

so in your case: 所以在你的情况下:

int deviceNumber = atoi(buffer + 2);

buffer[2]是一个char ,而不是char * ,因此您不能取消引用它。

I tried to fix it yourself, but I do not go. 我试图自己修复它,但我没有去。

Well, the expression buffer[2] is of type char . 好吧,表达式buffer[2]的类型为char You cannot dereference a char . 您不能取消引用char Perhaps you meant … 也许你的意思是……

buffer + 2

which is equivalent to 相当于

&buffer[2]

?

That will compile, but as an argument to atoi it is wrong: atoi requires a zero-terminated string that contains at least one digit, and a pointer to the last element of buffer can at best be a pointer to a terminating null-byte (with no digits). 可以编译,但是作为atoi的参数是错误的: atoi需要一个以零结尾的字符串,该字符串至少包含一个数字,并且指向buffer最后一个元素的指针充其量只能是一个指向结尾的空字节的指针(没有数字)。

Perhaps this is what you intended: 也许这就是您想要的:

atoi( buffer )

Or if you want a digit that's stored at index 2: 或者,如果您希望将数字存储在索引2中:

buffer[2] - '0'

(C++ guarantees that the character codes of decimal digits are consecutive). (C ++保证十进制数字的字符代码是连续的)。

Or if that char value is directly your integer value: 或者,如果该char值直接是您的整数值:

buffer[2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM