Noob question: Passing a string into a function

Another day, another question. :slight_smile:

I'm looking to have a string, ranging in size from 7 to 15 characters (specifically, an IP address), passed into a function so I can confirm it's a valid IP address. I have been reading online and have seen that you can't directly pass a string into a function, just use an asterisk to point to an existing string (I think).

Here's the function:

int IPValidator(char * IPAddress)
// This will confirm the validity of a given IP address.
// It checks for octets and if the number is under 255.
// returns 1 if a problem, 0 if OK
{
// first, look for octets. Distinguished by a period.
// if we don't have three periods, bail. 
// If we do, record location. We'll need it later.
  int FirstOctetLoc = IPAddress.indexOf('.');
  int SecondOctetLoc = IPAddress.indexOf('.', FirstOctetLoc+1);
  int ThirdOctetLoc = IPAddress.indexOf('.',SecondOctetLoc+1);
  if (FirstOctetLoc == -1 || SecondOctetLoc == -1 || ThirdOctetLoc == -1) {
    return 1;
  }
  char IPAddressBuffer[IPAddress.length()];
  IPAddress.toCharArray(IPAddressBuffer,IPAddress.length());
// First Octet
  String FirstOctetString = IPAddress.substring(0,FirstOctetLoc);
  char FirstOctetChar[4];
  FirstOctetString.toCharArray(FirstOctetChar,4);
  int FirstOctet=atoi(FirstOctetChar);
// Second Octet
  String SecondOctetString = IPAddress.substring(FirstOctetLoc+1,SecondOctetLoc);
  char SecondOctetChar[4];
  SecondOctetString.toCharArray(SecondOctetChar,4);
  int SecondOctet=atoi(SecondOctetChar);
// Third Octet
  String ThirdOctetString = IPAddress.substring(SecondOctetLoc+1,ThirdOctetLoc);
  char ThirdOctetChar[4];
  ThirdOctetString.toCharArray(ThirdOctetChar,4);
  int ThirdOctet=atoi(ThirdOctetChar);
// Fourth Octet
  String FourthOctetString = IPAddress.substring(ThirdOctetLoc+1,IPAddress.length());
  char FourthOctetChar[4];
  FourthOctetString.toCharArray(FourthOctetChar,4);
  int FourthOctet=atoi(FourthOctetChar);
  if (FirstOctet > 255 || SecondOctet > 255 || ThirdOctet > 255 || FourthOctet > 255){
    return 1;
  }
  else {
    return 0;
  }
}

If I add this to my code, I get this error:
error: request for member 'length' in 'IPAddress', which is of non-class type 'char*'

What did I do wrong this time? :slight_smile:

strings, lower case 's' are not the same as Strings upper case 'S'.

Mark

you are passing a pointer to a string (not a String which is an object) and so you cannot use String object methods like indexOf

holmes4:
strings, lower case 's' are not the same as Strings upper case 'S'.

Mark

I'm discovering that. :slight_smile:

In this case, it is a String with a capital S.

HugoPT:
you are passing a pointer to a string (not a String which is an object) and so you cannot use String object methods like indexOf

So, is it possible to parse a String in a function at all? If so, how can I do this?

So, is it possible to parse a String in a function at all?

Yes.

If so, how can I do this?

First, you need to pass a String (rather than a string) to the function, which needs to be designed to accept a String (rather than a string).

OK, passing a String instead of a string worked. At least, it passed compiling. :slight_smile:

int IPValidator(String IPAddress)
// This will confirm the validity of a given IP address.
// It checks for octets and if the number is under 255.
// returns 1 if a problem, 0 if OK
{
// first, look for octets. Distinguished by a period.
// if we don't have three periods, bail. 
// If we do, record location. We'll need it later.
  int FirstOctetLoc = IPAddress.indexOf('.');
  int SecondOctetLoc = IPAddress.indexOf('.', FirstOctetLoc+1);
  int ThirdOctetLoc = IPAddress.indexOf('.',SecondOctetLoc+1);
  if (FirstOctetLoc == -1 || SecondOctetLoc == -1 || ThirdOctetLoc == -1) {
    return 1;
  }
  char IPAddressBuffer[IPAddress.length()];
  IPAddress.toCharArray(IPAddressBuffer,IPAddress.length());
// First Octet
  String FirstOctetString = IPAddress.substring(0,FirstOctetLoc);
  char FirstOctetChar[4];
  FirstOctetString.toCharArray(FirstOctetChar,4);
  int FirstOctet=atoi(FirstOctetChar);
// Second Octet
  String SecondOctetString = IPAddress.substring(FirstOctetLoc+1,SecondOctetLoc);
  char SecondOctetChar[4];
  SecondOctetString.toCharArray(SecondOctetChar,4);
  int SecondOctet=atoi(SecondOctetChar);
// Third Octet
  String ThirdOctetString = IPAddress.substring(SecondOctetLoc+1,ThirdOctetLoc);
  char ThirdOctetChar[4];
  ThirdOctetString.toCharArray(ThirdOctetChar,4);
  int ThirdOctet=atoi(ThirdOctetChar);
// Fourth Octet
  String FourthOctetString = IPAddress.substring(ThirdOctetLoc+1,IPAddress.length());
  char FourthOctetChar[4];
  FourthOctetString.toCharArray(FourthOctetChar,4);
  int FourthOctet=atoi(FourthOctetChar);
  if (FirstOctet > 255 || SecondOctet > 255 || ThirdOctet > 255 || FourthOctet > 255){
    return 1;
  }
  else {
    return 0;
  }
}

String has a toInt() method that is faster than copying the char array and then calling atoi() on the char array.