1

I have an array called numbers and I want to test if a certain number is included in that array.

void loop(){
  if (arrayIncludeElement(numbers, mynumber)){
    // Do something
  } else {
    // Do something else
  }
}

I have written a version of the arrayIncludeElement function, but it don't seem to work.

boolean arrayIncludeElement(array, element) {
  for (int i = 0; i < sizeof(array); i++) {
    if (array[i] == element) {
      return true;
    }
  }
  return false;
}

ERROR MESSAGES

  • sketch:5: error: 'array' was not declared in this scope
  • sketch:5: error: 'element' was not declared in this scope
  • sketch:5: error: initializer expression list treated as compound expression
  • sketch:3: error: redefinition of 'boolean arrayIncludeElement'
  • sketch:5: error: 'boolean arrayIncludeElement' previously defined here
  • sketch.ino: In function 'void loop()':
  • sketch:11: error: 'arrayIncludeElement' cannot be used as a function
  • sketch.ino: At global scope:
  • sketch:16: error: redefinition of 'boolean arrayIncludeElement'
  • sketch:5: error: 'boolean arrayIncludeElement' previously defined here
  • sketch:16: error: 'array' was not declared in this scope
  • sketch:16: error: 'element' was not declared in this scope

EDIT I updated the function to this.

boolean arrayIncludeElement(int array[], int element);
boolean arrayIncludeElement(int array[], int element) {
  for (int i = 0; i < (sizeof(array)/4); i++) {
    if (array[i] == element) {
      return true;
    }
  }
  return false;
}

But now I get this error

  • sketch.ino: In function 'void loop()':
  • sketch:8: error: 'arrayIncludeElement' was not declared in this scope
  • sketch:10: error: expected `;' before '}' token

Line 8 being this

if (arrayIncludeElement(numbers, 5){

and Line 10 being this

} else {

2 Answers2

1

You are not mentioning the data type of that arguments. You have to mention the arguments. Then you have to declare the function prototype.

 boolean arrayIncludeElement(int array[], int element);
 boolean arrayIncludeElement(int array[], int element) {
 for (int i = 0; i < max; i++) {
      if (array[i] == element) {
          return true;
      }
    }
  return false;
 }

Edit: As like Dmitri said, pass the no of elements in the array as a function argument. Don't use the sizeof.

Then declartion of function, must be placed before where it is used. So in this place the function prototype before the function void loop().

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
0

It seems that this question is getting closer to How do I determine the size of my array in C?

BUT, in the general case, you do have to provide the length of the array or use some guard element (like null-terminated strings). Somewhere up the call chain you must know the size of the array, don't you?

A small example to show why sizeof() may not be correct:

int size_test(char b[])
{
    char a[10];
    char foo[] = {'a', 'b', 'c', 'd', 'e'};
    char *bar = malloc(20);
    printf("sizeof a = %ld, sizeof b = %ld, sizeof foo = %ld, sizeof bar = %ld\n", sizeof(a), sizeof(b), sizeof(foo), sizeof(bar));
}
int main()
{
    char b[12];
    size_test(b);
}

which, on my (64 bit) machine, prints

sizeof a = 10, sizeof b = 8, sizeof foo = 5, sizeof bar = 8

Community
  • 1
  • 1
drRobertz
  • 3,490
  • 1
  • 12
  • 23