How to pass an array to a function?

Please tell me how to pass an array {"+380951xxxxxx","+380952xxxxxx","+380953xxxxxx"} to a function okPhone ? I only transfer the first part ("+380951xxxxxx") of the entire array.

char* adminNumber[]={"+380951xxxxxx","+380952xxxxxx","+380953xxxxxx"};

String currStr = "dfgdfg dfg fdgf dfg dffg +380953xxxxxx fg dfg df ";
String phone = "";
String ddd = "";



boolean isStringMessage = false;

String okPhone(char *strNumbers[], String strPhone)
{
for (byte i = 0; i < (sizeof(strNumbers) / sizeof(strNumbers[0])); i++) {
     if (strPhone.indexOf(strNumbers[i]) > -1) 
     { 
      isStringMessage = true;
      phone = strNumbers[i];
      return phone;
      break;
     }
    }
}



void setup()
{
Serial.begin(115200);


ddd = okPhone(adminNumber,currStr);
Serial.print("ddd: ");Serial.println(ddd);



}

void loop()
{

}

Passing arrays to functions.

thanks, but that didn't help. I couldn't find anywhere how to correctly pass an array of text characters. In my example, only first part of the array is passed.

I'll start by saying that I'm not very good at C so I don't know how inefficient or how problematic this code could be but this also works:

String adminNumber[]={"+380951xxxxxx","+380952xxxxxx","+380953xxxxxx"};

String currStr = "dfgdfg dfg fdgf dfg dffg +380953xxxxxx fg dfg df ";
String phone = "";
String ddd = "";

boolean isStringMessage = false;

String okPhone(String strNumbers[], String strPhone)
{
  Serial.println(strNumbers[0]);
  Serial.println(strNumbers[1]);
  Serial.println(strNumbers[2]);
  return "asd";
}


void setup()
{
Serial.begin(115200);
ddd = okPhone(adminNumber,currStr);
Serial.print("ddd: ");Serial.println(ddd);
}

void loop()
{

}

Running this should print the 3 numbers in the serial monitor.

Thanks, but with your version, the array is incorrectly defined and the construction (sizeof(strNumbers) / sizeof(strNumbers[0])) does not work. This construct determines the number of elements in the array. I need to determine the length for the loop.

need to pass in how many elements..
maybe try..

char* adminNumber[]={"+380951xxxxxx\0","+380952xxxxxx\0","+380953xxxxxx\0"};

String currStr = "dfgdfg dfg fdgf dfg dffg +380953xxxxxx fg dfg df ";
String phone = "";
String ddd = "";



boolean isStringMessage = false;

String okPhone(char *strNumbers[],int howMany, String strPhone)
{
for (byte i = 0; i < howMany; i++) {
     if (strPhone.indexOf(strNumbers[i]) > -1) 
     { 
      isStringMessage = true;
      phone = strNumbers[i];
      return phone;
      break;
     }
    }
}



void setup()
{
Serial.begin(115200);


ddd = okPhone(adminNumber,3, currStr);
Serial.print("ddd: ");Serial.println(ddd);



}

void loop()
{

}

good luck.. ~q

The "C++ Way" would be to let the compiler figure out the array size for you:

using arrayPtr = const char * const;

const char * const adminNumber[] = {"+380951xxxxxx", "+380952xxxxxx", "+380953xxxxxx"};
const char currStr [] = "dfgdfg dfg fdgf dfg dffg +380953xxxxxx fg dfg df ";

template<size_t N>
const char * okPhone(arrayPtr (&strNumbers)[N], const char strPhone[]) {
  for (auto ptr : strNumbers) {
    //Serial.println(ptr);
    if (strstr(strPhone, ptr) != nullptr) {
      return ptr;
    }
  }
  return nullptr;
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  const char * result = okPhone(adminNumber, currStr);
  Serial.print("String \'");
  Serial.print(currStr);
  Serial.print("\' ");
  if (result != nullptr) {
    Serial.print("contains \'");
    Serial.print(result);
    Serial.println("\'");
  } else {
    Serial.println("does not contain any adminNumber");
  }
}

void loop() {
}

Output in Serial Monitor:

String 'dfgdfg dfg fdgf dfg dffg +380953xxxxxx fg dfg df ' contains '+380953xxxxxx'

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