strchr ?

When I run the following sketch,
I get

41
8
07676438

in the Serial Monitor window.

Why am I getting 07676438 and not 1 as per strchr - C++ Reference

#include <stdio.h>
#include <string.h>


char GSMbuffer[] = "12345678901234567-->07676438<--1234567890";
char phoneNumber[] = "07676438";

void setup() 
{

  Serial.begin(115200);



  Serial.println(strlen(GSMbuffer));
  Serial.println(strlen(phoneNumber));
  Serial.println(strchr(phoneNumber, '0'));

}

void loop() {}

Because you’re not doing the same :wink:

char GSMbuffer[] = "12345678901234567-->07676438<--1234567890";
char phoneNumber[] = "07676438";

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

  Serial.println(strlen(GSMbuffer));
  Serial.println(strlen(phoneNumber));
  Serial.println(strchr(phoneNumber, '0') - phoneNumber + 1);
}

void loop() {}

See the difference?

strchr() returns a pointer to the character :wink:

Because you did not read very closely. strchr() returns a pointer to that character. That is the memory address.

And the index you should actually get is 0 since in C we start counting at 0. If you did this:

(strchr(phoneNumber, '0') - &phoneNumber[0])

you would get the index you are looking for. This subtracts the address of the found element from the address of the first element and adjusts it for the size of the elements.

And yeah, I agree, start counting at 0 :smiley:

Sigh.

Pointers get me every time. I never fully understand them.

Thank you.