how does this code turn a char* into an integer???

Hello,

while learning about using arrays of char for sequences of characters (formerly known as "strings") ;-))

I came across this democode

char TextLine[] = "MyIdentfier=98";
const char EqualSign[] = "=";

void setup() {
  Serial.begin(115200);
  int position = strstr(TextLine, EqualSign) - TextLine + strlen(EqualSign);
  Serial.print("Number will start at position "); 
  Serial.println(position);
  Serial.print("Number is "); 
  Serial.println(TextLine + position);
}

void loop() {}

out of curiosity I tried a small deviation just out-commenting

int position = strstr(TextLine, EqualSign) //- TextLine + strlen(EqualSign);

char TextLine[] = "MyIdentfier=98";
const char EqualSign[] = "=";

void setup() {
  Serial.begin(115200);
  int position = strstr(TextLine, EqualSign) //- TextLine + strlen(EqualSign);
  Serial.print("Number will start at position "); 
  Serial.println(position);
  Serial.print("Number is "); 
  Serial.println(TextLine + position);
}

void loop() {}

and then the compiler complaints
invalid conversion from 'char*' to 'int' [-fpermissive]

without the "- TextLine + strlen(EqualSign);" the value would be wrong

But How the heck can adding "- TextLine + strlen(EqualSign);"
make the compiler satisfied??

best regards

Stefan

strstr returns a char *. The array name 'TextLine' is treated as a char *. Subtraction of these two pointers produces an integer. strlen returns a size_t which can be added to an integer.

The code does not convert anything to an integer.

TheMemberFormerlyKnownAsAWOL:
The code does not convert anything to an integer.

that's a typical answer of IBM-= ;-))

The code prints the contents of the character array beginning after the '=' position.

Again, it does not convert anything to an integer.

This is a language translation misunderstanding. The conversion that the OP is referring to, is in the single line in question, not the entire sketch.

aarg:
This is a language translation misunderstanding. The conversion that the OP is referring to, is in the single line in question, not the entire sketch.

The question has already been answered. Reply #1.