Expected primary-expression before 'int' error

Hi everyone, I'm using 2d arrays for the first time and I'm stuck on this error:
expected primary-expression before 'int'.

In this project, I've a 2d array with 2 rows per column. The first row is the millimeters that my sensor is away from an object, and the second row is the ADC value that the sensor reads out.
What I'm trying to do here is to see which second row (ADC value) has the same value as x (my sensor readings), and then print the value of the respective first row.

Thank you for your answers.

Code:

#define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)

void setup() {
   Serial.begin(9600); // start the serial port
    int y;
    int nRow;
    int myArray[y][nRow] = { /*All milimeters by there respective ADC value*/
        {40, 506,},
        {41, 496,},
        {42, 487,},
        {43, 481,},
        {44, 471,},
        {45, 465,},
        {46, 457,},
        {47, 449,},
        {48, 442,},
        {49, 434,},
        {50, 428,},
        {51, 422,},
        {52, 414,},
        {53, 408,},
        {54, 406,},
        {55, 397,},
        {56, 391,},
        {57, 385,},
        {58, 381,},
        {59, 373,},
        {60, 369,},
        {61, 362,},
        {62, 358,},
        {63, 354,},
        {64, 346,},
        {65, 342,},
        {66, 340,},
        {67, 336,},
        {68, 328,},
        {69, 324,},
        {70, 319,},
        {71, 317,},
        {72, 313,},
        {73, 309,},
        {74, 305,},
        {75, 301,},
        {76, 299,},
        {77, 295,},
        {78, 291,},
        {79, 287,},
        {80, 283,},
        {81, 279,},
        {82, 274,},
        {83, 270,},
        {84, 268,},
        {85, 266,},
        {86, 262,},
        {87, 260,},
        {88, 256,},//////////
        {89, 254,},
        {90, 252,},
        {91, 248,},
        {92, 246,},
        {93, 244,},
        {94, 242,},
        {95, 238,},//////////
        {96, 235,},
        {97, 233,},
        {98, 229,},//////////
        {99, 227,},
        {100, 225,},
    };
}

void loop() {
int y;
int  x = analogRead(sensor);
 // Serial.println(x);
  if(int myArray[y][1] == x){
      Serial.print(myArray[y][0]);
        Serial.println(" milimeters");
    }
  delay(100);
}

What's that "int" doing there?
Also, what's the value of y?

(in future, if you get an error message, post it verbatim - it contains valuable clues. Also, two 'l's in "millimetres")

myArray is declared in setup() so its scope is limited to that function

You haven't defined the Y variable to have any value.

You do nothing to limit the value of x to be within the array bounds.

Same problem with this code, no value assigned for y or nRow

And nRow has no defined value.

@lucmomber
Step 1:

Does this what you expect?

constexpr byte sensor =  A0; // Sharp IR GP2Y0A41SK0F (4-30cm, analog)
const int myArray[][2] = { /*All millimeters by there respective ADC value*/
  //mm, ADC
  {40, 506,},
  {41, 496,},
  {42, 487,},
  {43, 481,},
  {44, 471,},
  {45, 465,},
  {46, 457,},
  {47, 449,},
  {48, 442,},
  {49, 434,},
  {50, 428,},
  {51, 422,},
  {52, 414,},
  {53, 408,},
  {54, 406,},
  {55, 397,},
  {56, 391,},
  {57, 385,},
  {58, 381,},
  {59, 373,},
  {60, 369,},
  {61, 362,},
  {62, 358,},
  {63, 354,},
  {64, 346,},
  {65, 342,},
  {66, 340,},
  {67, 336,},
  {68, 328,},
  {69, 324,},
  {70, 319,},
  {71, 317,},
  {72, 313,},
  {73, 309,},
  {74, 305,},
  {75, 301,},
  {76, 299,},
  {77, 295,},
  {78, 291,},
  {79, 287,},
  {80, 283,},
  {81, 279,},
  {82, 274,},
  {83, 270,},
  {84, 268,},
  {85, 266,},
  {86, 262,},
  {87, 260,},
  {88, 256,},//////////
  {89, 254,},
  {90, 252,},
  {91, 248,},
  {92, 246,},
  {93, 244,},
  {94, 242,},
  {95, 238,},//////////
  {96, 235,},
  {97, 233,},
  {98, 229,},//////////
  {99, 227,},
  {100, 225,},
};
//int y;
//int nRow;

void setup() {
  Serial.begin(9600); // start the serial port
}

int lookup(int needle)
{
  for (size_t i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)
  {
    if (myArray[i][1] <= needle)
    return myArray[i][0];
  }
  return 0;
}

void loop() {
  int  x = analogRead(sensor);
  Serial.print(x);
  Serial.print("\t");
  Serial.print(lookup(x));
  Serial.println("mm");
  delay(100);
}

gives something like

226	100mm
238	95mm
249	91mm
254	89mm
262	86mm
272	83mm
280	81mm
285	80mm

if not please explain what's not correct.

Yes it does! Can you please explain what "int lookup" does?

it is just a "lookup" function which takes a parameter "needle" as input and returns a result

That just counts the elements in your array (total array size divided by size of first element):

sizeof(myArray)/sizeof(myArray[0])

and than you compare in the for loop until you have found the entry

int lookup(int needle)
{
  for (size_t i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)  // for each element in the array
  {
    if (myArray[i][1] <= needle)                                   // is this entry <= the lookup value?
      return myArray[i][0];                                        // return the result
  }
  return 0;   // just as fallback, to have a "return" at the end of the function
}

Thank you very much!

Uncompiled, untested:

int lookup(int needle)
{
  for (auto& record : myArray)  // for each element in the array
  {
    if (record [1] <= needle)                                   // is this entry <= the lookup value?
      return record [0];                                        // return the result
  }
  return 0;   // just as fallback, to have a "return" at the end of the function
}

step 2

this should do the same, but uses 120 byte less RAM:

constexpr byte sensor =  A0; // Sharp IR GP2Y0A41SK0F (4-30cm, analog)
constexpr byte firstEntry = 40; // first entry in array is 40mm
const int myArray[] = { /*All millimeters by there respective ADC value*/
  //ADC
  506,  // 40,
  496,  // 41,
  487,  // 42,
  481,  // 43,
  471,  // 44,
  465,  // 45,
  457,  // 46,
  449,  // 47,
  442,  // 48,
  434,  // 49,
  428,  // 50,
  422,  // 51,
  414,  // 52,
  408,  // 53,
  406,  // 54,
  397,  // 55,
  391,  // 56,
  385,  // 57,
  381,  // 58,
  373,  // 59,
  369,  // 60,
  362,  // 61,
  358,  // 62,
  354,  // 63,
  346,  // 64,
  342,  // 65,
  340,  // 66,
  336,  // 67,
  328,  // 68,
  324,  // 69,
  319,  // 70,
  317,  // 71,
  313,  // 72,
  309,  // 73,
  305,  // 74,
  301,  // 75,
  299,  // 76,
  295,  // 77,
  291,  // 78,
  287,  // 79,
  283,  // 80,
  279,  // 81,
  274,  // 82,
  270,  // 83,
  268,  // 84,
  266,  // 85,
  262,  // 86,
  260,  // 87,
  256,  // 88,
  254,  // 89,
  252,  // 90,
  248,  // 91,
  246,  // 92,
  244,  // 93,
  242,  // 94,
  238,  // 95,
  235,  // 96,
  233,  // 97,
  229,  // 98,
  227,  // 99,
  225,  // 100,
};

void setup() {
  Serial.begin(9600); // start the serial port
}

int lookup(int needle)
{
  for (size_t i = 0; i < sizeof(myArray) / sizeof(myArray[0]); i++)
  {
    if (myArray[i] <= needle)
      return i + firstEntry;
  }
  return 0;
}

void loop() {
  int  x = analogRead(sensor);
  Serial.print(x);
  Serial.print("\t");
  Serial.print(lookup(x));
  Serial.println("mm");
  delay(100);
}

P.S.: you can mark answers with the heart to say thank you to helpers.
Additionally you can mark one answer as solution if you think your question is solved.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.