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 {
6
  • Shouldn't you specify the types for your function arguments? Commented Jan 13, 2015 at 8:40
  • 1
    Also, sizeof(array) wouldn't work in your function -- you need to pass the size separately. Commented Jan 13, 2015 at 8:43
  • The sizeof(array) works alright. The problem was clearly that I missed the datatypes for arguments to the function. Commented Jan 13, 2015 at 8:48
  • 1
    No, the sizeof(array) gives the number of bytes in a pointer to int (or whatever your array is of). It gives something, but not the right thing. Commented Jan 13, 2015 at 8:49
  • Okay. But I don't know the length of my array, so is there another approach? Commented Jan 13, 2015 at 8:53

2 Answers 2

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().

Sign up to request clarification or add additional context in comments.

4 Comments

@EmilBonneKristiansen Ok no Problem.
Remember that inside the function, array looks like a pointer -- the type doesn't specify the number of elements. sizeof(array) will give the number of bytes in the pointer, not the number of bytes in the whole array. He's got to pass the size separately.
I thought it worked for a moment, but it seems there is a new error.
Okay, A friend of mine made it work. I feel like I am not really used to this kind of strict programming. I marked this answer as correct, thx everyone.
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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.