Thread: Is value in array?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    2

    Is value in array?

    Hello there, I am programming Arduino board in Wiring language (C).
    I want to use unsigned long array and I want to check if value (element) exists in array.

    I have tried solution from: C - If value in array - Stack Overflow that was created for data type float.
    But I am unable to get it running for unsigned long data type...

    Can somebody help me please what to edit?
    Friend told me to don't divide arrays.... But idk why, when for float it was working correctly. I want to use like 6 or 10 values there in array. Is there some other method that I can use that is fast enough?

    My original code for Arduino (setup is working like main in pure C):
    arrays - Pastebin.com
    Output in UART (Serial monitor):
    I am awaiting output "ZHODUJE SA", because entry argument (value) is in array... But output is like it is not there.. int returned 0.
    Is value in array?-jwqb3tx-png

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can't do sizeof on an array once you've passed it to a function.
    Or rather, it stops telling you what you want to know.
    Code:
    unsigned long TEAM_RED[] = {151188, 22222, 33333};
    int valueinarray(unsigned long val, unsigned long arr[], size_t len);
    unsigned long abc = 33333;
    void setup() {
      Serial.begin(115200);
      Serial.println("Ready");
      if (valueinarray(abc, TEAM_RED, sizeof(TEAM_RED) / sizeof(TEAM_RED[0]) )) {
        Serial.println("ZHODUJE SA");
      } else {
        Serial.println("NIE");
      }
    }
     
    void loop() {
     
     
    }
    int valueinarray(unsigned long val, unsigned long arr[], size_t len)
    {
      int i;
      for (i = 0; i < len; i++)
      {
        if (arr[i] == val)
          return 1;
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That Stack Overflow solution is wrong: it tries to compute the size with sizeof, but it's invoking sizeof on a pointer instead of an array.

    Just have a third parameter that is the number of elements in the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 4
    Last Post: 05-30-2013, 05:45 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread