bigben
bigben

Reputation: 21

Invalid type argument of unary ‘*’

I have problem with my Arduino C++ code. 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.

Upvotes: 1

Views: 9985

Answers (3)

Tudor
Tudor

Reputation: 62459

The error comes from the fact that buffer[2] is a char, not a pointer. There is nothing to dereference here. If you are trying to turn a char representing a digit into the corresponding int value use:

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

Or generally if you want the last N-K chars of a char array use:

int deviceNumber = atoi(buffer + K);

so in your case:

int deviceNumber = atoi(buffer + 2);

Upvotes: 2

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145419

I tried to fix it yourself, but I do not go.

Well, the expression buffer[2] is of type char. You cannot dereference a 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).

Perhaps this is what you intended:

atoi( buffer )

Or if you want a digit that's stored at index 2:

buffer[2] - '0'

(C++ guarantees that the character codes of decimal digits are consecutive).

Or if that char value is directly your integer value:

buffer[2]

Upvotes: 1

Daniel Fischer
Daniel Fischer

Reputation: 183968

buffer[2] is a char, not a char *, so you cannot dereference it.

Upvotes: 2

Related Questions