2

In my program I have the following lines of code:

if (invest == {0, 1, 0, 0})
   {
      //code 
   }

However, upon attempting to compile the code, I receive the following error:

exit status 1
expected ';' before '}' token

"Invest" is a bool array originally defined with`

bool invest[] = {0, 0, 0, 0};

How can I prevent this error from ocurring?

3 Answers 3

2

You cannot compare like that... you have to do it one by one (and check the length of the array).

if ((sizeof(invest) / sizeof(bool) == 4) &&
    (invest[0] == 0) && 
    (invest[1] == 1) &&
    (invest[2] == 0) &&
    (invest[3] == 0)) 
{
     ...
}

If you need to do such a compare often (with possibly different values), you of course can always write a dedicated function passing the array, and the array to check against and check the length and use a loop for checking all values.

Note that if invest is a boolean array, it's best to use false and true instead 0 and 1.

2
  • :((((((((((((((
    – Excelseo
    Jun 11, 2017 at 22:55
  • @Excelseo you are not happy with my answer? Jun 11, 2017 at 23:16
1

Unfortunately you cannot do that.

In addition to Michel Keijzers, another possible way is to use binary manipulation of an integer.

Your declaration of the variable would look like this:

unsigned int invest = 0x00;    // B0000 or 0 would be acceptable also

(See https://www.arduino.cc/en/Reference/IntegerConstants for notation reference.)
So your if statement would be very close to what you had originally wrote.

if (invest == B0100)
{
   //code 
}

Using this method you cannot reference each bit in the same way as an array (ie invest[3]). Instead to view the 4th bit:

result = (invest && 0x08) >> 3;

To set the 4th bit you would need to write:

invest |= 0x08;    // ie. B1000

And to clear the 4th bit:

invest &= 0x07;    // ie. B0111
2
0

You have a syntax error in that code; in C99, you would need to declare the type of the anonymous array {0, 1, 0, 0}, for example via (bool[]){0, 1, 0, 0}. So the following code compiles ok in C:

int main() {
  char invest[] = {0, 1, 0, 0};
  if (invest == (char[]){0, 1, 0, 0})
    printf("Yes yes yes\n");
  else
    printf("No no no\n");
  return 0;
}

However, if you compile and run this code, it will output "No no no" rather than "Yes yes yes" because the logical test invest == (char[]){0, 1, 0, 0} says to compare the address of the invest array to the address of the anonymous array, and of course they differ.

Note, if you try to compile the above as a C++ program, the result will be “error: taking address of temporary array” due to different type checking.


All that said, the straightforward way to compare arrays of bytes or in some cases arrays in general is to use the memcmp() function. From its man page:

SYNOPSIS  
   #include <string.h>
   ...  
   int memcmp(const void *s1, const void *s2, size_t n);
DESCRIPTION  
   The  memcmp()  function compares the first n bytes (each
   interpreted as unsigned char) of the memory areas s1 and s2.  
RETURN VALUE  
   The memcmp() function returns an integer less than, equal to,
   or greater  than zero  if  the  first  n  bytes of s1 is found,
   respectively, to be less than, to match, or be greater than 
   the first n bytes of s2.

The following code compiles ok both in C99 and C++ and prints out "Yes yes yes.".

#include <stdio.h>
#include <string.h>
int main() {
  char invest[] = {0, 1, 0, 0};
  char others[] = {0, 1, 0, 0};
  if (memcmp(invest, others, sizeof invest)==0)
    printf("Yes yes yes.\n");
  else
    printf("No no no.\n");
  return 0;
}

Note, when compiling code like this in the Arduino IDE, you may be able to leave out one or both of the #include statements.

1
  • I think this will not work if the length of others is longer than invest, than only the elements of invest are checked (or in other words: you should check also if the length of invest equals the length of others). Jun 12, 2017 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.